2010-07-01 63 views
12

我想从S3容器中读取照片的几何图形。回形针可以从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 

回答

14

如果您使用S3作为存储机制,您不能使用上面的几何方法(它假定本地文件)。回形针可以从S3文件转换为本地临时文件与Paperclip::Geometry.from_file

这是我更新的代码:

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

#to_file在回形针3.0已被删除。 1。在该版本和更高版本中,使用'Paperclip :: Geometry.from_file(Paperclip.io_adapters.for(photo.styles [style]))' – 2014-09-23 18:42:04

+2

@IsaacBetesh这不适用于我。我得到以下错误:'Paperclip :: AbstractAdapter#路径委托给@ tempfile.path,但@tempfile为零:Paperclip :: NilAdapter'。仅供参考,我使用s3和雾宝石。 – npouillard 2014-11-18 14:19:06

+0

我直接使用S3(即aws-sdk gem),所以我不能说任何关于雾的明确信息,但是你的堆栈跟踪可能会包含一些线索。 – 2014-11-18 21:14:37

10

这适用于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