Update: Amazon S3, Paperclip and a Curious Case of Singapore Buckets

This is an update of my previous blog post, “How to get Paperclip and AWS S3
Singapore and European Buckets Working
”.

Since the post last year in December, Amazon had released its official AWS SDK
for ruby which is now available as the ‘aws-sdk’ rubygem.

Paperclip had also made an update in its core to directly support AWS-SDK over
marcel’s AWS-S3 gem.

This little guide is supposed to help you get quickly started uploading your
images into S3 via Paperclip and the new gem.

This guide works for Rails 3.x and above.

Step 1: Get your Gems

# Gemfile
gem 'aws-sdk'
gem 'paperclip'

Step 2: Create your Paperclip Model

Step 3: Create your Amazon S3 credentials. In /config/s3.yml create:

#/config/s3.yml
development:
  bucket: <name>-dev
  access_key_id:
  secret_access_key:

test:
  bucket: <name>-test
  access_key_id:
  secret_access_key:

production:
  bucket: <name>-production
  access_key_id:
  secret_access_key:

Step 4: Rewrite your Paperclip Model to look something like this:

# app/models/somemodel.rb
  has_attached_file :photo,
     :storage => :s3,
     :s3_credentials => "#{Rails.root}/config/s3.yml",
     :path => "/:style/:id/:filename",
     :s3_host_name => "s3-ap-southeast-1.amazonaws.com"

As part of the move from aws/s3 to aws-sdk, there’s no longer a need to do
interpolate for non-US buckets. The new :s3_host_name parameter is supposed
to remove that need. The parameter :url hence is now no longer needed.

Step 5: ???

Step 6: Profit

And you’re done!

Again, I hope you guys find this useful. If you do, please pay it forward by
sharing your own guides to getting things to work. You have no idea how
incredibly useful they are to others.

[References]