Paperclip issue with Amazon’s S3 european buckets – solved
Finally I have found a solution for the well known problem of thoughtbot’s Paperclip and Amazon’s S3 EU buckets.
- First of all you have to define the has_attached_file class method inside your class. I have something like:
class Photo < ActiveRecord::Base has_attached_file :img, :styles => { :thumb => ["35x35#", :jpg], :medium => ["150x", :jpg], :original => ["650x", :jpg] }, :storage => :s3, :s3_credentials => "#{Rails.root}/config/amazon_s3.yml", :s3_protocol => "https", :path => ":class/:id/:basename_:style.:extension", :bucket => "your_bucket_name_here", :urlĀ => ":s3_eu_url" end
- Next create an initializer (/config/initializers/paperclip.rb) and inside define a Paperclip interpolation for the :s3_eu_url (paperclip uses this to construct the url of the image when you display it).
Paperclip.interpolates(:s3_eu_url) do |att, style| "#{att.s3_protocol}://s3-eu-west-1.amazonaws.com/#{att.bucket_name}/#{att.path(style)}" end
- Now that you have correctly defined the urls that Paperclip generates, you have to explicitly change the default host that Paperclip connects to when you uploading an image to Amazon’s S3. Well, this is not a Paperclip issue at all. Paperclip uses the infamous AWS::S3 when it connects to Amazon, so the issue comes from misconfiguring the AWS. Inside the same initializer put the following code
module AWS module S3 DEFAULT_HOST = "s3-eu-west-1.amazonaws.com" end end
Paperclip configuration for Amazon S3
Paperclip initializer to handle Amazon S3 url’s
AWS patch for setting the correct Amazon’s S3 EU url
Now you have a fully working Amazon S3 backed image (and not only) uploading system working in Rails 3.0
Great, I’ll be looking to give this solution a try over the weekend. I had recently switched back to a US bucket because I had other things to develop.
Nice!Give it a try and let me know if that worked for you.
Dude! – This works.
Thank you so much.
Took a bit of finding on google after playing around with a few other solutions. Let’s hope this gets up the google rankings so other people can find it.
Glad I helped Jules!
Hi Gerry,
thx for this beautiful page:) This is good stuff and my site is working well with EU-Buckets…
Have a good time..
Axel
Hi Gerry,
Thanks it really works.
I also tried adding this to the environment instead.
AWS::S3::DEFAULT_HOST.replace s3-eu-west-1.amazonaws.com
– Claire
I’ve created a simple gist for these changes to put them in initializer.
https://gist.github.com/928462
Excellent post Gerry! Thanks for the gist Martin, it works perfect!
I have created storage module for Paperclip that uses official ‘aws-sdk’ gem. It supports all S3 locations, and new features like Server Side Encryption.
Works well, using it in production projects myself. https://github.com/igor-alexandrov/paperclip-aws
Thanks a lot ! You saved my day.
Maybe something has changed in aws-sdk
in `<module:AWS>': S3 is not a module (TypeError)
Got the same, S3 is not a module :-(
Excellent, thanks for this!
For a moment there I thought I was going to have to upload everything onto a US bucket.
Thanks! :)