Wednesday, May 20, 2015

Setting Up Amazon S3 along with paperclip gem

Assuming you have already installed paperclip gem. .
My OS : Windows

#AMAZON S3 SETUP :
1. Create new account
2. Create S3 bucket, name it.
3. Go to security credentials, It'll ask you to generate IAM user, generate it.
4. Click access keys & generate a new one. Note down access_key_id & secret_access key.
5. Click policies & add "Administrator access policy".

Note : Step 5 is very imp or you'll get access denied while uploading on heroku server.

#RAILS CODE SETUP:

Step 1 : Add this gem to your gem file :
gem 'aws-sdk', '< 2.0'

Note: it's important that you write it exactly the same as the version above 2.0 will give you errors later on.

Step 2 : Add these lines inside  # config > environment > production.rb

# config/environments/production.rb
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']
  }
}

Note : Don't replace anything in the above code.

Step 3 : Execute these 3 commands from the root directory of your app using terminal
$ heroku config:set S3_BUCKET_NAME=your_bucket_name
$ heroku config:set AWS_ACCESS_KEY_ID=your_access_key_id
$ heroku config:set AWS_SECRET_ACCESS_KEY=your_secret_access_key

Note : Replace your_bucket_name with name of your bucket
For ex: if your bucket_name is hello-world then command will be :
$ heroku config:set S3_BUCKET_NAME=hello-world
Do the same for the accesss key & secret access key that we obtain from Amazon.

Everything is ready ! Go have fun !


No comments:

Post a Comment