How to Connect to the AWS S3 API
In this lesson, we are going to create our API connector to the AWS using Fog. This will let us send our files to their storage engine so that we can take advantage of their CDN system.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this lesson, we are going to create our API connector to the AWS using Fog. This will let us send our files to their storage engine so that we can take advantage of their CDN system.

To do that, we have to create an initializer file for Fog. Go to config/initializers, and you'll see some existing files such as assets.rb, session_store.rb and mime_types.rb. The files inside this folder get called when the server starts.

For this application, we are going to create an initializer file called fog.rb. In this file, we're going to write this code:

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider => "AWS",
    :aws_access_key_id => ENV ['ACCESS_KEY_ID'],
    :aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
  }
  config.fog_directory = ENV['AWS_BUCKET']
  config.fog_public = false
end

large

In this code, we are creating a block, inside of which we are setting the credentials for fog. If you notice, we're not passing the actual value, but calling the ENV variable, that in turn takes the value associated with each variable from the application.yml file. Lastly, we are setting the fog_public value as false because we don't want this to be available for everyone.

Once you're done, we are a step closer to get our application working.