每次我在本地重新播种我的数据库时,都会在我的 Amazon S3 存储桶中创建重复的图像。我认为这是因为我没有正确播种,但我不知道正确的方法。我一直在使用显示的方法 here .我正在使用 Rails 4、Ruby 2、回形针 3.5.2 和 aws-sdk 1.20.0。
您可以在我的 seeds.rb
文件中看到下面的内容,我正在尝试将图像设置为已上传到存储桶中正确文件夹的图像的 url。但是,我认为在这里使用 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>" }
种子.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"
最佳答案
我参加这个派对已经很晚了,但我认为其他人可能仍然遇到同样的问题。如果您在删除模型之前将模型上的附件设置为 nil,回形针将从 S3 中删除它们。
关于amazon-s3 - 使用 Paperclip + Amazon S3 使用现有图像播种时防止重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19918996/