我遇到过同样的问题几次。我解决它的方法是创建两个模型,一个Image
模型和一个TempImage
模型,它继承自Image
模型。这要求您在桌子type
上有一列Image
。模型将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
模型应始终包含您的后期处理,因为这将在后台完成。
您总是可以覆盖一些方法以使其更简洁,但这可以让您更好地了解它的工作原理以及您需要做什么,以便您可以让它按照您的意愿工作。