Amazon’s S3 european buckets and Paperclip in Rails 3

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.

    Paperclip configuration for Amazon S3

  1. 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
    
  2. Paperclip initializer to handle Amazon S3 url’s

  3. 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
    
  4. AWS patch for setting the correct Amazon’s S3 EU url

  5. 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
    

Now you have a fully working Amazon S3 backed image (and not only) uploading system working in Rails 3.0

12 Comments Amazon’s S3 european buckets and Paperclip in Rails 3

  1. Kevin

    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.

    Reply
  2. Jules

    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.

    Reply
  3. Axel

    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

    Reply
  4. Claire

    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

    Reply
  5. Dean

    Excellent, thanks for this!
    For a moment there I thought I was going to have to upload everything onto a US bucket.
    Thanks! :)

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>