使用Paperclip + Amazon S3在使用现有图像播种时防止重复

时间:2013-11-12 00:57:44

标签: amazon-s3 ruby-on-rails-4 paperclip

每次我在本地重新播放数据库时,都会在我的Amazon S3存储桶中创建重复的图像。我认为这种情况正在发生,因为我没有正确播种,但我不知道这样做的正确方法。我一直在使用here显示的方法。我使用的是Rails 4,Ruby 2,paperclip 3.5.2和aws-sdk 1.20.0。

您可以在我的seeds.rb文件中看到,我正在尝试将图像设置为已上传到我的存储桶中正确文件夹的图像的网址。但是,我认为在这里使用open()会导致一个新的,相同的文件保存到同一个文件夹,通常是http://s3.amazonaws.com/BUCKET_NAME/restaurants/images/1/original/open-uri20131111-22904-xvzitl.?1384211739

编辑:所以我的存储桶同时存储了此文件以及http://s3.amazonaws.com/BUCKET_NAME/restaurants/images/1/original/NAME.jpg

非常感谢任何帮助!

模型

has_attached_file :image,
      :styles         => { :medium => "300x300>", :thumb => "100x100>" }

seeds.rb

Restaurant.create!( name:         ...,
                    description:  ...,
                    image:        open('https://s3.amazonaws.com/<BUCKET NAME>/restaurants/images/1/original/<NAME>.jpg') )

配置/初始化/ paperclip.rb

Paperclip::Attachment.default_options[:storage]         = :s3
Paperclip::Attachment.default_options[:s3_credentials]  = {
                                                            :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
                                                            :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
                                                          }
Paperclip::Attachment.default_options[:bucket]          = ENV['AWS_BUCKET']
Paperclip::Attachment.default_options[:url]             = ":s3_path_url"
Paperclip::Attachment.default_options[:path]            = "/:class/:attachment/:id/:style/:basename.:extension"
Paperclip::Attachment.default_options[:default_url]     = "https://s3.amazonaws.com/<BUCKET NAME>/images/missing.png"

1 个答案:

答案 0 :(得分:1)

我在派对上已经很迟了,但我认为其他人可能仍然遇到同样的问题。如果在删除模型之前将模型上的附件设置为nil,则回形针将从S3中删除它们。