Skip to content

Amazon S3 Setup

Cindy Tan edited this page Jul 4, 2018 · 5 revisions

Step 1

Browse to Amazon S3 and sign in to the console using company's credentials. Then under the Storage service, click S3 to log in to the S3 console.

Step 2

Now create a bucket and give it a name follow by the project code and environment (eg. cb00-test). You will need to create for 3 different environments: test, staging, and production. Region should be US West(Oregon). We don't use additional options so click next, next to finish.

STEP 3

If we now navigate to Your Security Credentials found under our account name, we can find the S3 access key there, but the public key is hidden so we can copy it from previous apps configs.

Step 4

Now that we have the keys we can configure them in Heroku as follows

S3_BUCKET     = BUCKET_NAME_FROM_STEP_2
S3_REGION     = REGION_FROM_STEP_2
S3_ACCESS_KEY = ACCESS_KEY_FROM_STEP_3
S3_SECRET_KEY = SECRET_KEY_FROM_STEP_3

Step 5

To configure this on our local machine, we'll use a gem called fog.

Lets make sure we have gem 'fog' on our GemFile.

Now on the config/initializers directory add a fog.rb file and add the following code

CarrierWave.configure do |config|
  if Rails.env.production?
    config.fog_credentials = {
      provider: 'AWS',
      aws_access_key_id: ENV['S3_ACCESS_KEY'],
      aws_secret_access_key: ENV['S3_SECRET_KEY'],
      region: ENV['S3_REGION']
    }

    config.fog_directory = ENV['S3_BUCKET']
    config.fog_public = true
    config.fog_attributes = { 'Cache-Control' => 'max-age=315576000' }
  end

  # Use local storage if in development or test
  config.storage = Rails.env.production? ? :fog : :file
end
Clone this wiki locally