自定义Paperclip ::带有AWS S3的处理器

时间:2014-04-30 10:23:22

标签: ruby-on-rails amazon-s3 paperclip

如果上传本地存储,此代码可以正常工作,但有时它们在S3上,因此不可能仅source: "#{File.expand_path(src.path)}[0]"。如何使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

3 个答案:

答案 0 :(得分:1)

所以,根据谈话。事实证明,路径必须是一个完整的URL而不是相对路径。因此,这一行看起来像这样:

success = Paperclip.run('convert', parameters, source: src.url, dest: File.expand_path(dst.path)

因为,OP已经用if.. else回答了他的问题。我认为这是检查附件是在本地文件还是在CDN上的更好方法。

P.S。:我还了解到Paperclip.run(..)方法实际上可以找到并下载文件进行处理,如果它是一个网址,而不在开发人员的最后进行IO操作。

答案 1 :(得分:0)

嗯我没有得到你的问题,但对于Amazon S3 READ / WRITE,这是有用的......

这是我的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::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

所以在此配置之后,任何带有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)

答案 2 :(得分:0)

看起来这是解决方案!

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