Rails 应用程序无法使用 Paperclip 和 Mongoid 将上传的图像保存到 Amazon S3
我尝试在 Amazon S3 实例中存储与 Coupon
对象关联的图像。我的 Rails 3.1 应用程序使用 Mongoid 进行文档存储,并且我不打算引入 Paperclip(通过 mongoid-paperclip)来存储 Amazon S3 上的优惠券图像。
我已在 Amazon S3 上创建了一个权限组并添加了一个用户;有效的权限已添加到我的应用程序中(我可以验证,因为如果删除或更改权限,我会收到错误),但是当我尝试保存文件时,文件的信息存储在数据库中,但文件未上传。如果我从等式中删除 mongoid-paperclip,文件也不会存储在本地(尽管我确实看到它们存在于本地计算机上的临时文件夹中并通过 ImageMagick 进行处理)。
模型
我的 Coupon
对象嵌入了许多 Image
对象,如下所示:
class Coupon
include Mongoid::Document
include Mongoid::Timestamps
# Relationships
embeds_one :image, as: :imageable
# Database Schema
field :name
field :description
field :expires, type: Date
# Validation
validates :name, :description, :presence => true
end
class Image
include Mongoid::Document
include Mongoid::Paperclip
include Mongoid::Timestamps
# Relationships
embedded_in :imageable, polymorphic: true
has_mongoid_attached_file :file,
:path => ':id/:style.:extension',
:storage => :s3,
:s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
:styles => {
:original => ['920x920>', :jpg]
}
end
我在控制台或日志中没有看到 Paperclip 的任何输出,并且无法确定如何启用此类输出。与正在上传的文件相关的唯一记录信息如下,在成功更新属性后重定向页面之前:
|命令::识别-format%wx%h'/var/folders/ff/vxzlz741287dsr006bv2s59c0000gn/T/stream20111022-80997-o1pqk.png[0]'
|命令::转换 '/var/folders/ff/vxzlz741287dsr006bv2s59c0000gn/T/stream20111022-80997-o1pqk.png[0]' -resize "920x920>" '/var/folders/ff/vxzlz741287dsr006bv2s59c0000gn/T/stream20111022-80997-o1pqk20111022-80997-5z9phe.jpg'
I'm trying to store images associated with a Coupon
object in an Amazon S3 instance. My Rails 3.1 application uses Mongoid for document storage, and I'm not attempting to introduce Paperclip (via mongoid-paperclip) to store images for coupons on Amazon S3.
I've created a permission group on Amazon S3 and added a user; the valid permissions have been added to my application (which I can verify, because if I remove or alter the permissions, I receive an error), but when I attempt to save a file, the file's information is stored in the database, but the file is not uploaded. If I remove mongoid-paperclip from the equation, files are not stored locally either (although I do see that they exist in a temp folder on my local machine and are processed via ImageMagick).
Models
My Coupon
objects embeds many Image
objects as such:
class Coupon
include Mongoid::Document
include Mongoid::Timestamps
# Relationships
embeds_one :image, as: :imageable
# Database Schema
field :name
field :description
field :expires, type: Date
# Validation
validates :name, :description, :presence => true
end
class Image
include Mongoid::Document
include Mongoid::Paperclip
include Mongoid::Timestamps
# Relationships
embedded_in :imageable, polymorphic: true
has_mongoid_attached_file :file,
:path => ':id/:style.:extension',
:storage => :s3,
:s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
:styles => {
:original => ['920x920>', :jpg]
}
end
I do not see any output from Paperclip in my console or logs and cannot determine how to enable such output. The only information logged in relation to the file being uploaded is as follows, immediately before the page is redirected after successfully updating attributes:
| Command :: identify -format %wx%h '/var/folders/ff/vxzlz741287dsr006bv2s59c0000gn/T/stream20111022-80997-o1pqk.png[0]'
| Command :: convert '/var/folders/ff/vxzlz741287dsr006bv2s59c0000gn/T/stream20111022-80997-o1pqk.png[0]' -resize "920x920>" '/var/folders/ff/vxzlz741287dsr006bv2s59c0000gn/T/stream20111022-80997-o1pqk20111022-80997-5z9phe.jpg'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎是本地 ImageMagick 安装提供的识别命令的问题。您的系统上安装了 ImageMagick 的库吗?我曾经遇到过类似的问题,似乎从brew 安装 ImageMagick 解决了这个问题。仅供参考:我的问题是由错误的符号链接引起的,当我从源代码编译 ImageMagick 时,识别命令(和其他命令)根本没有正确链接。
This seems to be a problem with the identify command provided by your local ImageMagick install. Do you have ImageMagick's libraries installed on your system? I had a similar problem once and it seems that installing ImageMagick from brew fixed it up. FYI: My problem was caused by bad symbolic links, the identify command (and others) simply weren't linked correctly from when I compiled ImageMagick from source.
也许,这段代码没有使用回调。
Paperclip 使用 after_save 的回调来保存图像。
您应该使用 Mongoid 的级联回调。
http://mongoid.org/en/mongoid/docs/callbacks.html
Maybe, This code is not using callbacks.
Paperclip use the callback of after_save to save a image.
You should use cascade callbacks of Mongoid.
http://mongoid.org/en/mongoid/docs/callbacks.html