【问题标题】:Can paperclip read photo geometry off an S3 bucket?回形针可以从 S3 存储桶中读取照片几何图形吗?
【发布时间】:2011-03-10 19:13:47
【问题描述】:

我想从我的 S3 容器中读取照片的几何形状。

当它在我的本地时,这有效:

def photo_geometry(style = :original)
  @geometry ||= {}
  @geometry[style] ||= Paperclip::Geometry.from_file photo.path(style)
end

但是当我将模型切换到 S3 时它似乎不起作用。有什么建议吗?

更大的故事是,我正在尝试编写一些代码,允许我从 S3 中检索照片,允许用户裁剪它们,然后将它们重新上传回 S3 仍然由回形针分配。

编辑:

这是返回的错误:

Paperclip::NotIdentifiedByImageMagickError: photos/199/orig/greatReads.png is not recognized by the 'identify' command.
from /Users/daniellevine/Sites/hq_channel/vendor/gems/thoughtbot-paperclip-2.3.1/lib/paperclip/geometry.rb:24:in `from_file'
from /Users/daniellevine/Sites/hq_channel/app/models/photo.rb:68:in `photo_geometry'
from (irb):1

【问题讨论】:

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


    【解决方案1】:

    如果您使用 S3 作为存储机制,则不能使用上述几何方法(它假定为本地文件)。 Paperclip 可以使用Paperclip::Geometry.from_file 将 S3 文件转换为本地 TempFile:

    这是我更新的代码:

    def photo_geometry(style = :original)
      @geometry ||= {}
      @geometry[style] ||= Paperclip::Geometry.from_file(photo.to_file(style))
    end
    

    【讨论】:

    • #to_file 在回形针 3.0.1 中被删除。在该版本及更高版本中,使用Paperclip::Geometry.from_file(Paperclip.io_adapters.for(photo.styles[style]))
    • @IsaacBetesh 这对我不起作用。我收到以下错误:Paperclip::AbstractAdapter#path delegated to @tempfile.path, but @tempfile is nil: Paperclip::NilAdapter。仅供参考,我将 s3 与雾宝石一起使用。
    • 我直接使用 S3(即 aws-sdk gem)所以我不能说任何关于雾的确切信息,但你的堆栈跟踪可能会包含一些线索。
    【解决方案2】:

    这适用于 s3 和本地

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

    【讨论】:

      【解决方案3】:

      我或多或少有完全相同的问题,但这里没有一个答案对我有用,但确实如此:

      # helper method used by the cropper view to get the real image geometry
      def image_geometry(style = :original)
        @geometry ||= {}
        @geometry[style] ||= Paperclip::Geometry.from_file open("https:" + image.url(style))
      end
      

      【讨论】:

        猜你喜欢
        • 2012-09-10
        • 2013-09-09
        • 1970-01-01
        • 2017-10-01
        • 2010-12-10
        • 2017-11-01
        • 2014-01-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多