Updating Paperclip path filenames from server to s3

I have a paperclip instance that I am moving my files to another area. The files were originally saved on my server and just gave the file name based on the id of the post created and the original id. Now I move them to s3 and want to update the filenames so they work accordingly. I am setting up my paperclip config like this:

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

      

I updated the original filenames of the files to be unique and moved them to my s3 instance. Unfortunately now I can't pull files from s3 and I think it's because paperclip is using the wrong path for filenames. The one based on the default path, which is now set using my config file. I want to be able to update the file_name field for files so that the path is correct for new files and I can load them accordingly. Is there a way to call the paperclips hash function based on my secret and hash_data directly so that I can update those file_name fields and be able to fetch those records now? Everything that has been downloaded since migrating from my original servers seems to be working as expected.

+3


source to share


1 answer


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

Go to the rails console for example. rails c

and then get an object for the model you have an attachment to, eg. u = User.find(100)

...

Now enter u.profile_pic.url

to get url or u.profile_pic_file_name

to get filename.

To see the effect of other parameters (like your old parameters), 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

      



Likewise, it p.path

will show the path to the local file with whatever parameters you choose.

In 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 provide you with what you want :)

+2


source







All Articles