2012-03-16 63 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控制檯中的文件的權限,他們是合適的。