Steps of S3 bucket implementation in Rails(3.x)

Amazon Simple Storage Service, e.g. S3, is a “highly durable and available store” and can be used to reliably store application content such as media files, static assets and user uploads. It allows you to offload your entire storage infrastructure and offers better scalability, reliability, and speed than just storing files on the file-system.

It is an online storage web service offered by Amazon Web Services and provides storage through web services interfaces (REST, SOAP etc.)

Step#1

Install  aws-s3 gem by adding in Gemfile

gem ‘aws-s3’

Run bundle

bundle install

Step#2

To get AWS S3 bucket ‘Access Key ID’ and ‘Secret Access Key’ go to the “http://aws.amazon.com/s3”

Create s3_credentials.yml file under config directory and enter your Amazon S3 credentials

development:
bucket: dev-bucket-name
access_key_id: xxxxx
secret_access_key: xxxxx

test:
bucket: test-bucket-name
access_key_id: xxxxx
secret_access_key: xxxxx

production:
bucket: prod-bucket-name
access_key_id: xxxxx
secret_access_key: xxxxx

Step#3

Open your model file that will hold the attachment and modify it as follows

has_attached_file :image,
:styles =>{ :thumb => “100×100”, :medium => “200×200”, :large => “600×400” },
:storage => :s3,
:s3_credentials => “#{RAILS_ROOT}/config/s3_credentials.yml”,
:path => “:attachment/:id/:style.:extension”,
:bucket => ‘yourbucket’

Step#4

In view, to display the image

<%= image_tag Profile.image.url(:thumb) %>