我正在尝试重命名s3中的大量文件 - 将当前回形针has_attached_file
:path
从stuff/:id_:updated_at_:style.:extension
更改为stuff/:id_:counter_:style.:extension
,其中:counter
是与图像在同一模型中的字段。
我对如何重命名所有文件并不是最模糊的 - 最好是在rake任务中。
顺便说一下,每次将新文件保存到记录中时,我都会递增:counter
。
这是Rails 3和截至本帖的最新Paperclip。
有什么想法吗?
谢谢!
答案 0 :(得分:5)
这是我的解决方案:
# 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_
。
像魅力一样!
答案 1 :(得分:1)
试一下paperclip的刷新rake任务。我用它来生成新的样式,我猜它会改变你的路径变化吗?
https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation