4
votes

I used to have this buggy Paperclip config:

class Photo < ActiveRecord::Base

  has_attached_file :image, :storage => :s3,
                    :styles => { :medium => "600x600>", :small => "320x320>", :thumb => "100x100#" },
                    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                    :path => "/:style/:filename"
end

This is buggy because two images cannot have the same size and filename. To fix this, I changed the config to:

class Photo < ActiveRecord::Base

  has_attached_file :image, :storage => :s3,
                    :styles => { :medium => "600x600>", :small => "320x320>", :thumb => "100x100#" },
                    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                    :path => "/:style/:id_:filename"
end

Unfortunately this breaks all URLs to attachments I've already created. How can I update those file paths or otherwise get the URLs to work?

2

2 Answers

4
votes

I ended up doing this manually with the aws-s3 gem:

Photo.all.map{|p| [p.image.path(:thumb), "/thumb/#{p.id}_#{p.image_file_name}"]}.each do |p|
  if AWS::S3::S3Object.exists? p[0], bucket_name
    AWS::S3::S3Object.rename p[0], p[1], bucket_name
  end
end

(Of course I had to repeat it for each attachment style)

2
votes

You can run Photo.find_each { |photo| photo.image.reprocess! } from a migration or even within the console.

You may have a rake task installed to do this as well, depending on how you installed paperclip. You can try running rake paperclip:refresh CLASS=Photo. Don't forget to set RAILS_ENV as well if necessary.

If you want the rake tasks and don't have them, the file is here, and can be dropped directly into lib/tasks