Ammar
Ammar

Reputation: 1091

Rails 3 - Amazon S3 Paperclip EU Problem

I'm using:

Paperclip 2.3.16
Rails 3.0.9
Ruby 1.9.2
AWS - S3 0.6.2

I'm trying to use paperclip the upload to the EU (Ireland) based bucket. I have the following in my model:

has_attached_file :image, :styles => { :grid => '90x128#', :list => '140x200#', :original => '400x548'},
            :storage => :s3,
                      :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                      :url  => 'flyers/:id/:style/:basename.:extension',
                      :path => 'flyers/:id/:style/:basename.:extension',
                      :bucket => 'fsight'

In my environment.rb I have set the write to use the AWS/s3 Default Host to the relevant EU one by using:

require "aws/s3"
AWS::S3::DEFAULT_HOST.replace "s3-eu-west-1.amazonaws.com"

This works fine, and it allows me to upload the images, and I can verify the image upload / delete using the AWS Management consoler.

However, I have a problem when trying to display the images on my site. The images do not load and I have identified the cause, as the URL generated uses the old default host. Eg:

What it should be: https://s3-eu-west-1.amazonaws.com/fsight/flyers/50/full/4759543368588654950.jpg

What it actually is: http://s3.amazonaws.com/fsight/flyers/50/full/4759543368588654950.jpg?1314801178

As you can see, it uses the old default host.

I tried placing:

Paperclip.interpolates(:s3_eu_url) do |att, style|
    "#{att.s3_protocol}://s3-eu-west-1.amazonaws.com/#{att.bucket_name}/#{att.path(style)}"
end

But then started receiving the following error:

 wrong number of arguments (0 for 1)

Extracted source (around line #9):

 <img src= <%= @event.image.url(:original) %>

I know Paperclip has some issues with using EU Buckets, but could anybody help me with this?

Upvotes: 11

Views: 7995

Answers (4)

KrauseFx
KrauseFx

Reputation: 11741

I added

Paperclip::Attachment.default_options[:s3_host_name] = 's3-eu-west-1.amazonaws.com'

to paperclip.rb in the initializers folder and it works fine for me.

Upvotes: 5

toashd
toashd

Reputation: 1002

You don't need to work around the EU Problem anymore.

The default aws-s3 storage backend in paperclip was replaced by the AWS SDK for Ruby, that is also the amazon recommended way when working with AWS.

Just insert

gem 'aws-sdk'

into your Gemfile and run bundle install.

If you want something like https://s3-eu-west-1.amazonaws.com/some_path_goes_here, try to configure your model's has_attached_file with the following options

:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:s3_permissions => :private,
:s3_protocol => 'https',
:s3_host_name => 's3-eu-west-1.amazonaws.com',
:path => ":filename"

If you don't want to use https you can remove the :s3_protocol and if you want to change the region, option :s3_host_name is the right way to go. You can also put this into a configuration file.

Hope this helps.

Upvotes: 31

Darme
Darme

Reputation: 7083

Same problem here, just solved passing the following option to has_attached_file:

:url => ':s3_domain_url' 

For more info see here http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3 :

Normally, this won't matter in the slightest and you can leave the default (which is path-style, or :s3_path_url). But in some cases paths don't work and you need to use the domain-style (:s3_domain_url).

Upvotes: 2

Arnaud Leymet
Arnaud Leymet

Reputation: 6122

Did you try this workaround?

Paperclip et les European S3 buckets

Or even this one?

Paperclip, S3, and European Buckets

Upvotes: 8

Related Questions