使用S3时,Paperclip在本地查找文件以进行重新处理

时间:2012-06-20 13:58:52

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

我正在使用paperclip将文件上传到s3存储。文件上传后,我尝试使用Jcrop裁剪它。当logo.reprocess!运行时它试图在本地而不是在s3上查找文件并给出No such file or directory错误。这是相关代码

  has_attached_file :logo,
    styles: { thumb: "145x75#", large: "500x500" },
    :storage => :s3,
    :s3_credentials => "#{Rails.root}/config/s3.yml",
    :processors => [:cropper]

  def cropping_logo?
    !logo_crop_x.blank? && !logo_crop_y.blank? && !logo_crop_w.blank? && !logo_crop_h.blank?
  end

  def logo_geometry(style = :original)
    @geometry ||= {}
    path = (logo.options[:storage]==:s3) ? logo.url(style) : logo.path(style)
    @geometry[style] ||= Paperclip::Geometry.from_file(path)
  end

  def reprocess_logo
      logo.reprocess!
  end

module Paperclip
  class Cropper < Thumbnail
    def transformation_command
      if crop_command
        crop_command + super.sub(/ -crop \S+/, '')
      else
        super
      end
    end

    def crop_command
      target = @attachment.instance
      if target.cropping_logo?
      " -crop '#{target.logo_crop_w.to_i}x#{target.logo_crop_h.to_i}+#{target.logo_crop_x.to_i}+#{target.logo_crop_y.to_i}'"
      end
    end
  end
end

这是一些帮助的堆栈跟踪

/usr/local/ruby/lib/ruby/1.9.1/fileutils.rb:1423:in `stat'
/usr/local/ruby/lib/ruby/1.9.1/fileutils.rb:1423:in `block in fu_each_src_dest'
/usr/local/ruby/lib/ruby/1.9.1/fileutils.rb:1439:in `fu_each_src_dest0'
/usr/local/ruby/lib/ruby/1.9.1/fileutils.rb:1421:in `fu_each_src_dest'
/usr/local/ruby/lib/ruby/1.9.1/fileutils.rb:391:in `cp'
paperclip (3.0.2) lib/paperclip/io_adapters/attachment_adapter.rb:53:in `copy_to_tempfile'
paperclip (3.0.2) lib/paperclip/io_adapters/attachment_adapter.rb:44:in `cache_current_values'
paperclip (3.0.2) lib/paperclip/io_adapters/attachment_adapter.rb:6:in `initialize'
paperclip (3.0.2) lib/paperclip/io_adapters/registry.rb:29:in `new'
paperclip (3.0.2) lib/paperclip/io_adapters/registry.rb:29:in `for'
paperclip (3.0.2) lib/paperclip/attachment.rb:91:in `assign'
paperclip (3.0.2) lib/paperclip/attachment.rb:279:in `reprocess!'

1 个答案:

答案 0 :(得分:1)

我有同样的问题。我认为这与Paperclip v3代码有关。

我在我的Gemfile中指定使用旧版本。

# Gemfile
gem "paperclip", "~> 2.7.0" 

裁剪工作。我不确定这对你来说是否是一个很好的解决方案,但它暂时适用于我。