【问题标题】:Custom Paperclip::Processor with AWS S3带有 AWS S3 的自定义回形针::处理器
【发布时间】:2014-05-16 21:13:04
【问题描述】:

如果上传内容存储在本地,则此代码可以正常工作,但有时它们位于 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

【问题讨论】:

  • 您可以在 s3 使用 url 用于任何上传的图像,但它们是否被上传,如果是,那么您可以简单地使用 @image.avatar.url 访问,其中头像是带有 has_attached_file:avatar 的回形针附件。 .请参阅下文了解更多信息
  • src.path 上传到 s3 时会给出什么?是相对路径吗?还是完整的路径?
  • @Surya 这是一个相对路径 "/assets/files/000/000/583/original/Screen_Shot_2014-05-13_at_16.59.42.png"
  • 你可以试试:src.url 吗?如果可以,请执行以下操作:stackoverflow.com/a/2517286/645886 然后处理它。老实说,我不认为是否可以在 CDN 上处理图像。您必须在服务器上拥有该图像(如果它是您运行操作的本地系统则更好)。
  • @Surya 感谢它的帮助。我已经回答了这个问题以说明需要做什么。您还可以提交答案并获得赏金

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


【解决方案1】:

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

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

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

P.S.:我还了解到Paperclip.run(..)方法实际上可以定位并下载一个文件,如果它是一个url,而不需要在开发者端进行IO操作。

【讨论】:

    【解决方案2】:

    好吧,我没有很好地回答您的问题,但对于 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
    

    我的配置/初始化程序/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)
    

    【讨论】:

    • 请查看 Paperclip::Processor 的功能。我将默认处理的图片存储到 s3 没有问题。
    【解决方案3】:

    看起来这就是解决方案!

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-24
      • 2018-07-06
      相关资源
      最近更新 更多