2012-03-16 66 views
3

我有一个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 
+0

拉姆达将适合你吗?看看这个http://stackoverflow.com/questions/8590822/apply-processor-with-paperclip-if-condition-its-true – mohamagdy 2012-03-16 18:45:38

+0

也许这是新文件的访问权限问题?也许你应该设置BucketOwnerFullControl选项? [例如](http://stackoverflow.com/questions/7616810/amazon-s3-upload-with-public-permissions) – 2012-03-22 14:26:17

回答

0

回形针只是不想存储在具有相同附件的两个桶中。因此,我将样式分隔到同一个存储桶中的公共/私人“文件夹”,并为每种样式赋予不同的权限。

这里是我的模型代码:

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