I have a GalleryPhoto model with a paperclip attachment that gets processed into a number of styles. Some styles need to be publicly readable; some need to be private.
Here’s my model:
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
Here’s my paperclip initializer:
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
I have four buckets as such:
private.myapp_development
private.myapp_production
public.myapp_development
public.myapp_production
The private.xxx buckets shouldn’t be publicly accessible, but the public.xxx buckets should be publicly readable.
I can get the app to serve the styles that have been marked public in the public buckets, but I cannot do uploads or have my download action, which serves the private styles, work.
Here’s the log when I try to do an upload:
[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'
Here’s the log when I try to use my download action for the private styles:
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'
What am I missing?
rails 3.1.1
paperclip 2.7.0
aws-sdk 1.3.7
Paperclip just did not want to store in two buckets with the same attachment. So I separated out the styles into public/private “folders” in the same bucket, with different permissions for each style.
Here’s my model code:
When I check the permissions on the files in the s3 console, they are appropriate.