[英]Rename paperclip files with rake task
我已經在Rails項目的回形針中使用默認值上傳了一些圖像
path: ":class/:attachment/:id_partition/:style/:filename"
因此,所有文件都將上載到amazon s3存儲桶中。
現在我想要/需要將文件名更改為
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.rename(old_name, new_name, 'bucket_name')
另外,這可能會有所幫助: http : //blog.magmalabs.io/2015/11/25/rename-s3-assets-after-paperclip-hashing.html
在這里創建了一個github工作項目:
https://github.com/tclaus/Rename-S3-assets-after-paperclip-hashing
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.