3
votes

Every time I re-seed my database locally, duplicate images are being created in my Amazon S3 bucket. I think this is happening because I am not seeding correctly, but I don't know the proper way to do it. I've been using the method shown here. I'm using Rails 4, Ruby 2, paperclip 3.5.2, and aws-sdk 1.20.0.

You can see below in my seeds.rb file, I'm trying to set the image to the url of an image that has already been uploaded to the correct folder in my bucket. However, I think using open() here is causing a new, identical file to be saved to the same folder, usually something like http://s3.amazonaws.com/BUCKET_NAME/restaurants/images/1/original/open-uri20131111-22904-xvzitl.?1384211739.

EDIT: so my bucket will have both this file stored as well as http://s3.amazonaws.com/BUCKET_NAME/restaurants/images/1/original/NAME.jpg

Would really appreciate any help!

model

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') )

config/initializers/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

1 Answers

1
votes

I'm pretty late to the party on this one but I figure others may still be having the same problem. If you set the attachments on your models to nil before deleting them paperclip will delete them from S3.