开发者

Paperclip S3 download remote images

How I can download a remote image (http protocol, the url is in the image_remote_url attr开发者_StackOverflow中文版ibute) 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 ?


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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜