Setting up AWS for Carrierwave

Jadam
gSchool Stories
2 min readDec 22, 2014

--

Its only a handful of steps to get this up and running, but I found the docs were’t quite as explicit as I would have liked. In addition to having to add the fog gem you’ll also likely find you’ll want the dotenv gem aswell.

The fog gem can be dropped in your main group, but the dotenv-rails gem will need to be added with groups =>[:development, :test] if you don’t already have that group.

gem ‘fog’
gem ‘dotenv-rails’, :groups => [:development, :test]

In your terminal run:

$ bundle

At the top level of your application’s tree create a .env and .env.example file.

In your .env file is where you’ll put your AWS keys. My file looks something like this:

S3_KEY=s0m3_b1g_10ng_str!ng_0f_stuff
S3_SECRET=s0m3_b1g_10ng_str!ng_0f_stuff
S3_BUCKET="bucket_file_name"

You’ll be copying everything from your .env file into your .evn.example file, but without all the secret values you filled in. The file only has the keys/variables and look something like the example below:

S3_KEY=
S3_SECRET=
S3_BUCKET=

This .env.example file is for anyone else who may be working off your code and lets them know what keys they’ll need to provide to get things running on their environment.

Next inside config/initializers create a carrierwave.rb file in which you’ll add:

CarrierWave.configure do |config|
config.fog_credentials = {
:provider => ‘AWS’,
:aws_access_key_id => ENV[‘S3_KEY’],
:aws_secret_access_key => ENV[‘S3_SECRET’],
}
config.fog_directory = ENV[‘S3_BUCKET’]
end

I’ve left out a few of the optional fields that are listed in Carrierwave’s docs. Then inside your XXXX_uploader.rb file uncomment the “storage :fog” option. Short of committing your changes that’s about it. One last step you’ll need to take if you’re using Heroku is setting those keys.

when pushing to Heroku you’ll need to set your AWS secrets separately:

$ heroku config:set S3_KEY=s0m3_b1g_10ng_str!ng_0f_stuff S3_SECRET=s0m3_b1g_10ng_str!ng_0f_stuff S3_BUCKET=yourbucketname

--

--

Jadam
gSchool Stories

A chronic creative in Denver currently exploring street art in society though the @DenverStreetart project. All while trying to live a sustainable lifestyle.