[英]Rails Paperclip S3 rename thousands of files?
我正在尝试重命名 s3 中的很多文件-将当前回形针has_attached_file
:path
从stuff/:id_:updated_at_:style.:extension
更改为stuff/:id_:counter_:style.:extension
,其中:counter
是与图像相同的 model 中的字段。
我对如何重命名所有文件一无所知——最好是在 rake 任务中。
顺便说一句,每次将新文件保存到记录时,我都会增加:counter
。
这是 Rails 3 和本文发布时最新的回形针。
有任何想法吗?
谢谢!
这是我的解决方案:
# 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 任务。 我用它来生成新的 styles 我猜它也会随着你的路径变化而发生变化?
https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.