使用回形针将 URL 中的图像上传到 AWS S3

Upload image from URL with Paperclip to AWS S3

我有一份产品清单。对于每个产品,我有一个 image_url 我想上传到我的 AWS S3 帐户并与之关联。

我可以通过表格手动完成,但是有大约 1500 种产品,我无法一一完成。

我的 Paperclip 配置如下所示:

config.paperclip_defaults = {
    :url => 'xxxx',
    :path => '/:class/:attachment/:id_partition/:style/:filename',
    :command_path => "/usr/bin/convert",
    :storage => :s3,
    :s3_credentials => {
      :bucket => "xxx",
      :access_key_id => "XXX",
      :secret_access_key => "XXXX"
    }
  }

这是我用来手动上传图片的代码

class Product < ActiveRecord::Base
  # This method associates the attribute ":image" with a file attachment
  has_attached_file :image, styles: {
    thumb: '100x100>',
    square: '200x200#',
    medium: '300x300>'
  }

  # Validate the attached image is image/jpg, image/png, etc
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

end

一切顺利,我可以在日志末尾看到:

[paperclip] saving /products/images/000/001/361/original/xxx.jpg
[AWS S3 200 1.107584 0 retries] put_object(:acl=>:public_read,:bucket_name=>"xxx",:content_length=>47766,:content_type=>"image/jpeg",:data=>Paperclip::UploadedFileAdapter: xxx.jpg,:key=>"products/images/000/001/361/original/xxx.jpg")  

[paperclip] saving /products/images/000/001/361/thumb/xxx.jpg
[AWS S3 200 0.444224 0 retries] put_object(:acl=>:public_read,:bucket_name=>"xxx",:content_length=>26502,:content_type=>"image/jpeg",:data=>Paperclip::FileAdapter: 3ef480ec0ebbef3921b4c074897fecc320150625-13674-roduas,:key=>"products/images/000/001/361/thumb/xxx.jpg")  

[paperclip] saving /products/images/000/001/361/square/xxx.jpg
[AWS S3 200 0.492781 0 retries] put_object(:acl=>:public_read,:bucket_name=>"xxx",:content_length=>36071,:content_type=>"image/jpeg",:data=>Paperclip::FileAdapter: 3ef480ec0ebbef3921b4c074897fecc320150625-13674-1xrcndz,:key=>"products/images/000/001/361/square/xxx.jpg")  

[paperclip] saving /products/images/000/001/361/medium/xxx.jpg
[AWS S3 200 0.553079 0 retries] put_object(:acl=>:public_read,:bucket_name=>"xxx",:content_length=>43927,:content_type=>"image/jpeg",:data=>Paperclip::FileAdapter: 3ef480ec0ebbef3921b4c074897fecc320150625-13674-8dq1h4,:key=>"products/images/000/001/361/medium/xxx.jpg")


但现在我想自动执行此任务(在为数据库做种时)。我添加了 open-uri 以从他们的 URL 中检索图像。

require 'open-uri'

class Product < ActiveRecord::Base
  # This method associates the attribute ":image" with a file attachment
  has_attached_file :image, styles: {
    thumb: '100x100>',
    square: '200x200#',
    medium: '300x300>'
  }

  # Validate the attached image is image/jpg, image/png, etc
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

  def image_from_url(url)
    self.image = URI.escape(url)
  end

end

我基本上做的是创建一个新产品,类似于:

Product.create! :name => "Test!!!"

然后尝试向其中添加图像:

Product.last.image_from_url "MY_URL"

我没有发现错误,但它不起作用。我可以在日志中看到每种格式(thumb、square、medium)的转换,但它不像以前那样推送到 AWS。

刚写完我的问题就找到了答案。 也许它可以帮助某人所以我还是发布了它。

错误是我没有更新属性图片。所以不要这样做:

Product.last.image_from_url "MY_URL"

我做到了:

Product.last.update_attribute :image, Product.last.image_from_url("MY_URL")

现在图像已生成 AND 上传到 AWS