views:

36

answers:

2

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  A: 

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

x1a4
This seems like the right approach, but it doesn't work. E.g., suppose an image's old path is "a/b.png" and I want to reprocess it to be "a/123_b.png". Since I have `:path => "/:style/:id_:filename"`, Paperclip looks for the attachment at "a/123_b.png" to reprocess and I get a `AWS::S3::NoSuchKey` error
Horace Loeb
oh, i see so even the original path would change.In that case you have to do it by hand, which is kind of a bummer.
x1a4
A: 

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)

Horace Loeb