Rails 4和paperclip - 停止:原始样式文件上传,从S3远程目录复制它 [英] Rails 4 and paperclip - Stop the :original style file upload to copy it from an S3 remote directory
问题描述
我使用Paperclip 4.0.2和我的应用程式上传图片。
I use Paperclip 4.0.2 and in my app to upload pictures.
因此我的文件模型有一个attached_file 附件
So my Document model has an attached_file called attachment
附件有几种样式,例如:medium,:thumb::facebook
The attachment has few styles, say :medium, :thumb, :facebook
在我的模型中,我停止样式处理,并且在后台作业中提取它。
In my model, I stop the styles processing, and I extracted it inside a background job.
class Document < ActiveRecord::Base
# stop paperclip styles generation
before_post_process
false
end
但原始样式文件仍上传!
$ b b
我想知道是否可以停止此行为并将文件复制到远程目录中的:original / filename.jpg
我的目标是使用已在S3中上传的文件/ temp /目录下,并将其复制到Paperclip需要它来生成其他样式的目录。
I would like to know if it's possible to stop this behavior and copy the file inside the :original/filename.jpg from a remote directory My goal being to use a file that has been uploaded in a S3 /temp/ directory with jQuery File upload, and copy it to the directory where Paperclip needs it to generate the others styles.
先谢谢你的帮助! p>
Thank you in advance for your help!
推荐答案
新答案:
获得上传到 flush_writes
方法,为了您的目的,它是 Paperclip :: Storage :: S3 模块。负责上传的行是:
paperclip attachments get uploaded in the flush_writes
method which, for your purposes, is part of the Paperclip::Storage::S3 module. The line which is responsible for the uploading is:
s3_object(style).write(file, write_options)
因此,通过monkey_patch,您可以将其更改为:
So, by means of monkey_patch, you can change this to something like:
s3_object(style).write(file, write_options) unless style.to_s == "original" and @queued_for_write[:your_processed_style].present?
编辑: 创建以下文件: config / initializers / decorators / paperclip.rb
Paperclip::Storage::S3.class_eval do
def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
retries = 0
begin
log("saving #{path(style)}")
acl = @s3_permissions[style] || @s3_permissions[:default]
acl = acl.call(self, style) if acl.respond_to?(:call)
write_options = {
:content_type => file.content_type,
:acl => acl
}
# add storage class for this style if defined
storage_class = s3_storage_class(style)
write_options.merge!(:storage_class => storage_class) if storage_class
if @s3_server_side_encryption
write_options[:server_side_encryption] = @s3_server_side_encryption
end
style_specific_options = styles[style]
if style_specific_options
merge_s3_headers( style_specific_options[:s3_headers], @s3_headers, @s3_metadata) if style_specific_options[:s3_headers]
@s3_metadata.merge!(style_specific_options[:s3_metadata]) if style_specific_options[:s3_metadata]
end
write_options[:metadata] = @s3_metadata unless @s3_metadata.empty?
write_options.merge!(@s3_headers)
s3_object(style).write(file, write_options) unless style.to_s == "original" and @queued_for_write[:your_processed_style].present?
rescue AWS::S3::Errors::NoSuchBucket
create_bucket
retry
rescue AWS::S3::Errors::SlowDown
retries += 1
if retries <= 5
sleep((2 ** retries) * 0.5)
retry
else
raise
end
ensure
file.rewind
end
end
after_flush_writes # allows attachment to clean up temp files
@queued_for_write = {}
end
end
现在原始文件未上传。如果您希望将原件直接上传到s3,请将原件转移到相应的最终位置,然后在您的模型中添加一些行,如下面我的origninal答案。
now the original does not get uploaded. You could then add some lines, like those of my origninal answer below, to your model if you wish to transfer the original to its appropriate final location if it was uploaded to s3 directly.
原始答案:
或许类似这样放置在使用after_create回调执行的模型中:
perhaps something like this placed in your model executed with the after_create callback:
paperclip_file_path = "relative/final/destination/file.jpg"
s3.buckets[BUCKET_NAME].objects[paperclip_file_path].copy_from(relative/temp/location/file.jpg)
感谢 https://github.com/uberllama
这篇关于Rails 4和paperclip - 停止:原始样式文件上传,从S3远程目录复制它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!