Paperclip S3 download remote images

最后都变了- 提交于 2019-12-11 01:48:01

问题


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 ?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/2150196/paperclip-s3-download-remote-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!