[英]Custom Paperclip::Processor with AWS S3
This code works fine if the uploads are stored locally, but sometimes they are on S3 so it's not possible to just source: "#{File.expand_path(src.path)}[0]"
. 如果上传文件存储在本地,则此代码可以正常工作,但有时它们在S3上,因此不可能仅提供
source: "#{File.expand_path(src.path)}[0]"
。 How do I make the Paperclip's run
method load images from S3 and replace them afterwards? 如何使Paperclip的
run
方法从S3加载图像并随后替换它们?
module Paperclip
class KskCrop < Processor
def initialize(file, options = {}, attachment = nil)
super
@crop = options
@format = File.extname(@file.path)
@basename = File.basename(@file.path, @format)
end
def make
src = @file
dst = Tempfile.new([@basename, @format])
dst.binmode
parameters = []
parameters << ":source"
parameters << "-crop '#{@crop[2]}x#{@crop[3]}+#{@crop[0]}+#{@crop[1]}'"
parameters << ":dest"
parameters = parameters.flatten.compact.join(' ').strip.squeeze(' ')
success = Paperclip.run('convert', parameters, source: "#{File.expand_path(src.path)}[0]", dest: File.expand_path(dst.path))
dst
end
end
end
So, as per the conversation. 因此,根据对话。 It turned out that path has to be a complete url instead of a relative path.
原来,路径必须是完整的url,而不是相对路径。 Hence, this line will look something like this:
因此,此行将如下所示:
success = Paperclip.run('convert', parameters, source: src.url, dest: File.expand_path(dst.path)
As, OP has already answered his question with an if.. else
. 因为,OP已经用
if.. else
回答了他的问题。 I think that's a better approach to check if the attachment is on a local file or on a CDN. 我认为这是检查附件是否在本地文件或CDN上的更好方法。
PS: I also learned that Paperclip.run(..)
method can actually locate and download a file for processing if it's a url without making an IO operations at developer's end. PS:我还了解到,如果
Paperclip.run(..)
是url,则实际上可以找到并下载文件进行处理,而无需在开发人员的末端进行IO操作。
well i didnt got your question well but for Amazon S3 READ/WRITE, this is what is use... 好吧,我并没有很好地回答您的问题,但是对于Amazon S3 READ / WRITE,这是使用的...
this is my s3.yml 这是我的s3.yml
development:
bucket: app_development
access_key_id: xxxxxxxxxxxxxx
secret_access_key: xxxxxxxxxxxxx+xxxxxxxxxxx
production:
bucket: app_production
access_key_id: xxxxxxxxxxxxxxxxx
secret_access_key: xxxxxxxxxxxx+xxxxxxxxxxxxx
my config/initializers/paperclip.rb 我的配置/初始化/paperclip.rb
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:id/:style/:filename'
##will store in the foll way shown below in the bucket u specify in s3.yml
##http://app_development.s3.amazonaws.com/videos/1/original/small.mp4
##if Rails.env == "production"
#S3_CREDENTIALS = { :access_key_id => ENV['S3_KEY'], :secret_access_key => ENV['S3_SECRET'], :bucket => "ourbucket"}
##else
S3_CREDENTIALS = Rails.root.join("config/s3.yml")
##end
so after this configuration,any paperclip model with has_attached_file:avatar will get uploaded on s3 因此,完成此配置后,任何具有has_attached_file:avatar的回形针模型都将在s3上载
##upload to s3..code snippet (can also use in _form.html.erb as f.file_field :avatar)
@video=Video.create!({ :avatar => File.new("#{Rails.root}/app/assets/images/dashboard/default_avatar.jpg")
})
##this is how u can display/access any uploaded object/model using url(which shows original unless you specify some styles -thumbnail,small)
@video.avatar.url
@video.avatar.url(:thumbnail)
@video.avatar.url(:small)
Looks like this is the solution! 看起来这是解决方案!
module Paperclip
class KskCrop < Processor
def initialize(file, options = {}, attachment = nil)
super
@crop = options
@format = File.extname(@file.path)
@basename = File.basename(@file.path, @format)
end
def make
src = @file
dst = Tempfile.new([@basename, @format])
dst.binmode
parameters = []
parameters << ":source"
parameters << "-crop '#{@crop[2]}x#{@crop[3]}+#{@crop[0]}+#{@crop[1]}'"
parameters << ":dest"
parameters = parameters.flatten.compact.join(' ').strip.squeeze(' ')
path = if @file.options && @file.options[:storage] == :s3
src.url
else
File.expand_path(src.path)
end
success = Paperclip.run('convert', parameters, source: path, dest: File.expand_path(dst.path))
dst
end
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.