Ruby on rails 使用rake任务重命名回形针文件
我在rails项目中使用了回形针来上传一些图像,使用默认设置Ruby on rails 使用rake任务重命名回形针文件,ruby-on-rails,amazon-s3,paperclip,file-rename,Ruby On Rails,Amazon S3,Paperclip,File Rename,我在rails项目中使用了回形针来上传一些图像,使用默认设置 path: ":class/:attachment/:id_partition/:style/:filename" 因此,所有文件都上传到AmazonS3存储桶中 现在我想/需要将文件名更改为 path: ':class/:id/:style/:hash.:extension' 这对于新上传的文件很有效,但现有文件将无法再找到。 所以我试着重复使用回形针耙刷新任务。 步骤: -使用旧路径加载所有附件,使用新路径保存,删除旧条目。
path: ":class/:attachment/:id_partition/:style/:filename"
因此,所有文件都上传到AmazonS3存储桶中
现在我想/需要将文件名更改为
path: ':class/:id/:style/:hash.:extension'
这对于新上传的文件很有效,但现有文件将无法再找到。
所以我试着重复使用回形针耙刷新任务。
步骤:
-使用旧路径加载所有附件,使用新路径保存,删除旧条目。
以下是我目前的研究结果:
desc "Move renamed files"
task :move_renamed_files => :environment do
klass = Paperclip::Task.obtain_class
names = Paperclip::Task.obtain_attachments(klass)
names.each do |name|
Paperclip.each_instance_with_attachment(klass, name) do |instance|
attachment = instance.send(name)
if attachment.exists?
print "."
else
# No hit on new location, trying old location
org_path = attachment.options[:org_path]
new_path = attachment.options[:path]
attachment.options[:path] = org_path
if attachment.exists?
# Save file with new name
puts "#{attachment.url}"
attachment.options[:path] = new_path
instance.save(:validate => false)
# THIS DOES NOT WORK
#if attachment.save
# puts "Save OK"
#else
# puts "Save failed"
#end
else
Paperclip::Task.log_error("#{instance.class}##{attachment.name}, #{instance.id}, #{attachment.url}")
end
end
end
end
你知道如何完成代码吗?
我完全陷进去了 我将深入研究AWS::S3库,因为您已经有了路径。如果你知道它是有效的,那就用最快的方法 AWS::S3::S3Object.renameold\u名称,新的\u名称,“bucket\u名称” 此外,这可能有助于:
已在此处创建github工作项目: