简体   繁体   English

Paperclip S3下载远程图像

[英]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 ? 我如何下载远程图像(http协议,url在image_remote_url属性中)并通过Paperclip将其保存为S3的附件?

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 ? “fetch_image”方法应该怎么样?

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/ 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). 这应该现在在paperclip(版本> 3.1.3)上完成工作(不是3.1.3,而是随后的事情)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM