Configuring Heroku, AWS S3 and Paperclip to work with a bucket outside of the USA

A functionality of adding images to new or existing products is a must for any ecommerce application. This feature is also included in Spree and with a quick configuration works very well within the USA.

 

Lately, we have been configuring a new Spree application (that comes with a Paperclip already included), running on Heroku, to work with an Amazon S3 bucket. This time, the AWS resource was located in Ireland, Europe. As usual, we have followed instructions available on the Heroku dev center. After adding Amazon Web Service Security Credentials and the bucket name as environment variables to the Heroku config and referring to these variables from Paperclip config we were already able to upload and remove the images to the S3. However, the images in our views were still missing. Instead, we were seeing the following XML message from the S3:

The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.

From the message description, we have deducted that the image links should refer to the proper Amazon region. The issues were the image links generated by the Paperclip. The link in the following form was not directing us to the EU resource:

s3.amazonaws.com/bucket_name/...

Few nodes below the error message the proper endpoint was specified and indeed changing the address in a web browser to the specified endpoint lead us to the resource uploaded via Paperclip to the S3.

bucket_name.s3.amazonaws.com/...

We started looking into the Paperclip documentation and searching for similar issues via google. We have found a suggestion of interpolating the link and moving the bucket_name into the subdomain, but this approach seemed really rough. After a little more research we have discovered the bucket is actually available via a different link as well.

s3-eu-west-1.amazonaws.com/bucket_name/...

That’s more like it! Using this point of access we wouldn’t have to mess with the link structure generated by the Paperclip, but only change the domain that we are referring to. In the end, simply expanding the production config with two additional lines did the trick.

config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
},

:s3_host_name => "s3-eu-west-1.amazonaws.com", # Added entry
:url => ":s3_host_name"                        # Added entry
}

This is how to make Heroku, Rails and Paperclip gem work with an Amazon S3 bucket located in a region different than the US.

search-icon close-icon social-facebook social-twitter social-linkedin close-icon menu-icon