Rails Paperclip S3重命名成千上万的文件?

我正在尝试重命名s3中的大量文件 – 更改当前的回形针has_attached_file :path来自stuff/:id_:updated_at_:style.:extension :path stuff/:id_:updated_at_:style.:extensionstuff/:id_:counter_:style.:extension ,其中:counter是与图像在同一模型中的字段。

我对如何重命名所有文件并不是最模糊的 – 最好是在rake任务中。

顺便说一下,每次将新文件保存到记录中时,我都会递增:counter

这是Rails 3和最新的Paperclip。

有任何想法吗?

谢谢!

这是我的解决方案:

 # This task changes all of the keys from the current format, # :id_:image_updated_at_:style, to :id_:image_counter_:style. # :image_counter is set arbitrarily at 1, since all records have # a default of 1 in that field (until they're updated). desc "One-time renaming of all the amazon s3 content for User.image" task :rename_s3_files, [:bucket] => :environment do |t, args| require 'aws/s3' cred = YAML.load(File.open("#{Rails.root}/config/s3.yml")).symbolize_keys! AWS::S3::Base.establish_connection! cred bucket = AWS::S3::Bucket.find(args[:bucket]) # Rename everything in the bucket, taking out the timestamp and replacing it with "1" bucket.each do |obj| arr = obj.key.split('_') obj.rename(arr[0] + '_1_' + arr[2]) end end 

它只是遍历存储桶中的所有文件,并根据此新架构重命名它们。 我将Paperclip路径中的:counter字段设置为默认值为1,因此新文件名中的_1_

奇迹般有效!

试试回形针的刷新rake任务。 我用它来生成新的样式,我猜它会改变你的路径变化吗?

https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation