【问题标题】:Upload image from URL with Paperclip to AWS S3使用 Paperclip 从 URL 上传图像到 AWS S3
【发布时间】:2023-03-29 10:55:02
【问题描述】:

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

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

我的回形针配置如下所示:

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"

我对此没有任何错误,但它不起作用。我可以在日志中看到每种格式(拇指、正方形、中等)的转换,但它不像以前那样推送到 AWS。

【问题讨论】:

    标签: ruby-on-rails image amazon-web-services amazon-s3 paperclip


    【解决方案1】:

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

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

    Product.last.image_from_url "MY_URL"
    

    我做到了:

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

    现在图像已生成上传到 AWS

    【讨论】:

      猜你喜欢
      • 2014-01-18
      • 2011-09-21
      • 2021-01-19
      • 2018-03-02
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-14
      相关资源
      最近更新 更多