Paperclip具有不同权限的不同样式

时间:2012-03-16 15:02:06

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

我有一个带有回形针附件的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存储桶应该是公开可读的。

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

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

[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'

当我尝试将下载操作用于私有样式时,这是日志:

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

1 个答案:

答案 0 :(得分:0)

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

这是我的型号代码:

  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控制台中检查文件的权限时,它们是合适的。