我已經跑了幾次這個相同的問題。我解決這個問題的方式是創建2個模型,一個Image
模型和一個TempImage
模型,它繼承了Image
模型。這要求您在Image
表上有type
列。 TempImage
模型將圖像保存到本地,然後當您直接從Image
模型訪問它並重新保存它時,它將遵循Image
模型中定義的任何內容,即Amazon S3。
例子:
# Will save in the database as a TempImage inside the Image table
temp = TempImage.create(:asset => File.new('some_path', 'r'))
# When you find it again through the Image model, it bypasses the type column
# so next time you save it, it is saved as an Image.
amazon = Image.find(temp.id)
amazon.save!
這是我的工作延遲:
class MoveToS3Job < Struct.new(:temp_revision_id)
def perform
upload = Image.find(temp_revision_id)
temp_path = File.expand_path("tmp/uploads/#{upload.asset_file_name}", Rails.root)
upload.asset = File.new(temp_path, 'r')
upload.save!
if File.exists?(temp_path) && !File.directory?(temp_path)
File.delete(temp_path)
end
rescue ActiveRecord::RecordNotFound
# If the record wasn't found, do some sort of
# error report, but don't keep it in the queue.
end
end
這裏是TempImage
型號:
class TempImage < Image
has_attached_file :asset, {
:path => ":rails_root/tmp/uploads/:basename_:updated_at.:extension"
}
end
那麼原來Image
型號:
class Image < ActiveRecord::Base
# Validations
validates :asset, :presence => true
# Paperclip
has_attached_file :asset, :styles => {
:preview => ['100x100#', :png],
:thumb => ['50x50#', :png]
},
:default_style => :thumb,
:storage => :s3,
:bucket => 'bucket-name',
:s3_credentials => File.expand_path('config/s3.yml', Rails.root),
:path => "photos/:id_partition/:style.:extension"
end
您的原始Image
模型應始終包含您的後處理,因爲這將在後臺完成。
您可以隨時覆蓋一些方法使其更清潔一些,但這會讓您更好地瞭解它是如何工作的以及您需要做什麼,以便您可以按照自己的需要進行工作。