gpt4 book ai didi

ruby-on-rails - 具有不同权限的不同样式的回形针

转载 作者:行者123 更新时间:2023-12-05 00:01:07 25 4
gpt4 key购买 nike

我有一个带有回形针附件的 GalleryPhoto 模型,它被处理成多种样式。有些样式需要公开可读;有些需要保密。

这是我的模型:

class GalleryPhoto < ActiveRecord::Base
has_attached_file :image,
:storage => :s3,
:bucket => ":bucket.myapp_#{Rails.env == 'production' ? 'production' : 'development'}",
:path => "images/galleries/:gallery_id/:id/:style_:id.:extension",
:url => "/images/galleries/:gallery_id/:id/:style_:id.:extension",
:s3_credentials => { :access_key_id => 'XXXXXXXXX', :secret_access_key => 'XXXXXXXXX' },
:s3_permissions => {
:thumbnail => :public_read,
:small => :public_read,
:medium => :public_read,
:large => :public_read,
:small_download => :private,
:original => :private
},
:styles => {
:thumbnail => {
:geometry => "80x80>"
},
:small => {
:geometry => "200x200>"
},
:medium => {
:geometry => "400x400>"
},
:large => {
:geometry => "600x600>"
},
:small_download => {
:geometry => "600x600"
}
}
end

这是我的回形针初始化程序:

Paperclip.interpolates :bucket do |attachment, style|
[:original, :small_download].include?(style) ? "private" : "public"
end

Paperclip.interpolates :gallery_id do |attachment, style|
attachment.instance.gallery_id
end

我有四个桶:

private.myapp_development
private.myapp_production
public.myapp_development
public.myapp_production

private.xxx 存储桶不应公开访问,但 public.xxx 存储桶应公开可读。

我可以让应用程序提供已在公共(public)存储桶中标记为公开的样式,但我无法进行上传或下载操作(提供私有(private)样式)。

这是我尝试上传时的日志:

[paperclip] Saving attachments.
[paperclip] saving images/galleries/242/22034/original_22034.jpg
(0.8ms) ROLLBACK
Completed 500 Internal Server Error in 8013ms

Errno::EPIPE (Broken pipe):
app/controllers/gallery_photos_controller.rb:13:in `create'

这是我尝试对私有(private)样式使用下载操作时的日志:

Sent file images/galleries/222/19515/original_19515.jpg (0.2ms)
Completed 500 Internal Server Error in 861ms

ActionController::MissingFile (Cannot read file images/galleries/222/19515/original_19515.jpg):
app/controllers/gallery_photos_controller.rb:22:in `download'

我错过了什么?

rails 3.1.1
paperclip 2.7.0
aws-sdk 1.3.7

最佳答案

Paperclip 只是不想将相同附件存储在两个桶中。所以我将样式分离到同一个存储桶中的公共(public)/私有(private)“文件夹”中,每种样式具有不同的权限。

这是我的模型代码:

  has_attached_file :image,
:storage => :s3,
:bucket => "myapp-#{Rails.env == 'production' ? 'production' : 'development'}",
:path => ":bucket/images/galleries/:gallery_id/:id/:style_:id.:extension",
:url => "/:bucket/images/galleries/:gallery_id/:id/:style_:id.:extension",
:s3_credentials => { :access_key_id => 'XXXXX', :secret_access_key => 'XXXXXXXXX' },
:s3_permissions => {
:thumbnail => :public_read,
:small => :public_read,
:medium => :public_read,
:large => :public_read,
:small_download => :private,
:original => :private
},
:styles => {
:thumbnail => {
:geometry => "80x80>"
},
:small => {
:geometry => "200x200>"
},
:medium => {
:geometry => "400x400>"
},
:large => {
:geometry => "600x600>"
},
:small_download => {
:geometry => "600x600"
}
}

当我在 s3 控制台中检查文件的权限时,它们是合适的。

关于ruby-on-rails - 具有不同权限的不同样式的回形针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9739671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com