Rails的回形针S3重命名数以千计的文件? [英] Rails Paperclip S3 rename thousands of files?
问题描述
我想重新命名了很多文件在S3 - 改变当前回形针 has_attached_file
:路径
从的东西/:ID_:updated_at_:风格:扩展
到的东西/:ID_:counter_:风格:扩展
,其中:计数
是在同一个模型作为图像领域
I'm trying to rename a lot of files in s3--changing the current paperclip has_attached_file
:path
from stuff/:id_:updated_at_:style.:extension
to stuff/:id_:counter_:style.:extension
, where :counter
is a field in the same model as the image.
我还没有就如何重命名的所有文件的foggiest - pferably在rake任务$ P $
I haven't the foggiest on how to rename all the files--preferably in a rake task.
顺便说一句,我会递增:计数
一个新的文件保存到记录每次
Incidentally, I'll be incrementing :counter
each time a new file is saved to the record.
这是Rails 3的和最新的回形针作为此张贴。
This is Rails 3 and the latest Paperclip as of this posting.
任何想法?
谢谢!
推荐答案
下面是我的解决办法:
# 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
它只是通过在桶中的所有文件,并根据这一新的模式重新命名它们。我设置:在回形针路径计数器字段默认为1,因此 _1 _
在新的文件名
工程就像一个魅力!
这篇关于Rails的回形针S3重命名数以千计的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!