回形针 + S3:将现有文件从一种 :path 格式迁移到另一种 [英] Paperclip + S3: Migrating existing files from one :path format to another
问题描述
我有一个带有 avatar
回形针附件的模型.它现在有一个简单的标准路径
I have a model with a avatar
paperclip attach. It has now a plain standard path
has_attached_file :avatar,
:path => "/:id-:style-:filename"
我需要进入一个混淆的地方
Which I need to move into an obfuscated one
has_attached_file :avatar,
:path => "/:id-:hash.:extension"
:hash_secret => 'asecuresecret'
新图像一切正常,但是如何将存储在 S3 中的现有文件迁移到新的路径格式,即重命名它们?
Everything's working fine with new images, but how do I migrate the existing files stored in S3 to the new path format, ie rename them?
我使用的是回形针 2.7 和 Rails 3.2
I'm using paperclip 2.7 and Rails 3.2
谢谢!!!
推荐答案
这个 rake 任务应该可以解决问题.我用 aws-sdk 1.5.2 和 ruby 1.9.3p194 尝试过.
This rake task should do the trick. I tried it with aws-sdk 1.5.2 and ruby 1.9.3p194.
new_key
应该映射到您的新回形针路径.不要忘记根据需要设置:acl
.
The new_key
should map to your new paperclip path. Don't forget to set :acl
according to your needs.
namespace :data do
desc 'aws images migration'
task :migrate_images do |t, args|
s3 = AWS::S3.new(:access_key_id => 'XXX', :secret_access_key => 'XXX')
bucket = s3.buckets['your-bucket-name']
bucket.objects.each do |object|
new_key = object.key.gsub(........)
new_object = bucket.objects[new_key]
object.copy_to new_object, {:acl => :public_read}
end
end
end
一旦您确定新文件是正确的,就应该手动删除原始文件或使用类似的任务.
The original file should be deleted manually or using a similar task, once you are sure the new file is correct.
这篇关于回形针 + S3:将现有文件从一种 :path 格式迁移到另一种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!