Paperclip + S3: Migrating existing files from one :path format to another

前端 未结 3 1466
不思量自难忘°
不思量自难忘° 2021-02-14 03:57

I have a model with a avatar paperclip attach. It has now a plain standard path

has_attached_file :avatar,
  :path => \"/:id-:style-:filename\"
<         


        
3条回答
  •  故里飘歌
    2021-02-14 04:12

    I would write a rake task (or just a plain script if you prefer, to be run in the rails context). If you're using the aws-s3 gem, iterate over the instances of the model which you know they have the old path format or try writing some condition on the filename to match them, and the move one by one.

    Model.find_in_batches(:batch_size => 500,
          :conditions => "avatar_filename like 'SOMETHING_MATCHING'") do |o|
      AWS::S3::S3Object.rename(old_path(o.avatar), o.avatar.url, 'BUCKET_NAME')
    end
    

    If you have already configured avatar with the new path definition, write a method that can build the old path based on the avatar properties.

    You can read the aws-s3 gem docs here to see how to establish a connection to your S3 account.

提交回复
热议问题