我正在尝试重命名 s3 中的许多文件——更改当前的回形针has_attached_file
:path
from stuff/:id_:updated_at_:style.:extension
to stuff/:id_:counter_:style.:extension
, where :counter
是与图像相同模型中的字段。
我对如何重命名所有文件一无所知——最好是在 rake 任务中。
顺便说一句,我会增加:counter
每次将新文件保存到记录中。
这是 Rails 3 和截至本文发布时的最新 Paperclip。
有任何想法吗?
Thanks!
这是我的解决方案:
# 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_
在新文件名中。
奇迹般有效!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)