簡體   English   中英

Rails Paperclip S3重命名數千個文件?

[英]Rails Paperclip S3 rename thousands of files?

我正在嘗試重命名 s3 中的很多文件-將當前回形針has_attached_file :pathstuff/: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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM