astropanic
astropanic

Reputation: 10939

Paperclip S3 download remote images

How I can download a remote image (http protocol, the url is in the image_remote_url attribute) and save it as an attachment to S3 via Paperclip ?

class Product < ActiveRecord::Base
  require 'open-uri'
  attr_accessor :image_remote_url
  has_attached_file :photo,
    :storage => :s3,
    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
    :path => ":class/:id/:style.:extension",
    :bucket => "my_bucket",
    :styles => {
      :icon => "32x32#",
  }

  def fetch_image
    # how should this method look ?
  end

end

How should the method "fetch_image" look ?

Upvotes: 2

Views: 3900

Answers (2)

Aditya Sanghi
Aditya Sanghi

Reputation: 13433

I'm not sure this is still useful for you or not, but in a pull request to paperclip just a few hours ago, I've managed to make this super easy.

def set_photo
  self.photo = URI.parse(self.image_remote_url)
end

This should do the job now on paperclip (version > 3.1.3) (not 3.1.3 but whatever comes after).

Upvotes: 2

jlfenaux
jlfenaux

Reputation: 3291

Here's a link to a page that explains exactly what you need.

http://trevorturk.wordpress.com/2008/12/11/easy-upload-via-url-with-paperclip/

I've implemented it successfully on my own site.

Upvotes: 6

Related Questions