Skip to main content

Configure amazon s3 with your rails app in just 5 mins

if you are looking to make use of your uploads using paperclip on AMAZON S3,then this is something that will help you.
i tried the same and wasted 5 days but its very easy by using ONLY aws-sdk gem and you can start uploading to S3.
 Before starting i assume few thing from you:
  1. You have a running rails app ready.
  2. You have an active AMAZON S3 account (get the credentials,you will need it now)
  3. You are using paperclip 3.5.1(i prefer)
  4. You uploads are saving to your system locally
short preview of the sequence you need to understand:
START -> get aws-sdk gem -> create new s3.yml -> configure an initializer-paperclip.rb using AMAZON S3 credentials ----END----------THAT'S IT

lets get started:
  1. Install 'aws-sdk' gem by including in gemfile and running bundle install.
Gemfile:

gem 'paperclip', '3.5.1'
gem "aws-sdk"
  1.  Create new yml file such as config/s3.yml and store your AMAZON S3 credentials as shown below:
development:
bucket:development_bucket
access_key_id: YOURACCESSKEY  
secret_access_key: YOURSECRETACCESSKEY 
production: 
bucket: production_bucket
access_key_id: YOURACCESSKEY
secret_access_key: YOURSECRETACCESSKEY

 3.You need an initializer file to override default url options using config/initializer/paperclip.rb 
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:id/:filename' 
S3_CREDENTIALS = Rails.root.join("config/s3.yml")  

4.add this to your image.rb or any other file using has_attached_file.
has_attached_file :avatar,
:storage =>:s3, 
:s3_credentials => S3_CREDENTIALS  
WE ARE DONE...start your uploads and check it on AWS MANAGEMENT CONSOLE in development bucket




Comments