Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating Paperclip path file names from on server to s3

I have a paperclip instance that I am migrating my files to a different area. Originally the files were stored on my server and just given a filename based on the id of the record created and the original id. Now I'm moving them to s3 and want to update the filenames to work appropriately. I setup my paperclip config like so:

:path => ":class/:attachment/:hash-:style.:extension",
    :url => ":s3_domain_url",
    :hash_secret => SECRET,
    :hash_data => ":class/:attachment/:id/:updated_at"

I updated the original records filenames for my files to be unique and moved them over to my s3 instance. Unfortunately now I am unable to pull down the files from s3 and I think it is because paperclip is using the wrong path for the filenames. One that is based off the path default that is now set using my config file. I want to be able to update my files file_name field so that the path is correct for the new files and I am able to download them appropriately. Is there a way to call paperclips hashing function based on my secret and hash_data directly so I can update those file_name fields and be able to pull those records now? Everything that has been uploaded since the move from my original servers seems to work appropriately.

like image 523
Ben Nelson Avatar asked Sep 07 '25 19:09

Ben Nelson


1 Answers

Say you have a model User with an attachment named profile_pic;

Go into the rails console eg. rails c and then get an object for the model you have the attachment on, eg. u = User.find(100).

Now type u.profile_pic.url to get the url or u.profile_pic_file_name to get the filename.

To see the effect of other options (for example your old options) you can do;

p = u.profile_pic # gets the paperclip attachment for profile_pic
puts p.url # gets the current url
p.options.merge!(url: '/blah/:class/:attachment/:id_partition/:style/:filename')
puts p.url # now shows url with the new options

Similarly p.path will show the local file path with whatever options you pick.

Long story short, something like;

User.where('created_at < some_date').map do |x| 
  "#{x.id} #{x.profile_pic_file_name} #{x.profile_pic.path}" 
end

should give you what you want :)

like image 101
Anko Avatar answered Sep 10 '25 17:09

Anko