Skip to content

Instantly share code, notes, and snippets.

@jendiamond
Forked from kkchu791/S3.md
Last active March 18, 2016 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jendiamond/b1624c05ed98824dba90 to your computer and use it in GitHub Desktop.
Save jendiamond/b1624c05ed98824dba90 to your computer and use it in GitHub Desktop.
Persisting Images

AWS- Setting up Carrierwave for S3

It's time to setup your AWS bucket so your photos can persist on the internet. Instead of pushing photos to your local machine, you will be pushing them to an Amazon Web Service cloud.

You will need your credit card for doing this. It's free to use but be forewarned, be careful not to push your AWS credentials up to any public domains or somebody could use your credit card.

Before we begin let's get our app setup for Amazon Web Services(AWS):

###Adding Fog Gem and Carrierwave Configs

The fog gem is the Ruby cloud services library that helps you send your pictures to your AWS account.

To install, add fog-aws to your Gemfile

#Gemfile
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'carrierwave'
+ gem 'fog-aws', '~> 0.7.6'

In your command line, bundle install

$ bundle install

Make a new file in config/initializers directory called carrierwave.rb and add configurations. This is the file that carrierwave will use to push your photos to AWS.

# config/initializers/carrierwave.rb

require 'carrierwave/storage/abstract'
require 'carrierwave/storage/file'
require 'carrierwave/storage/fog'

CarrierWave.configure do |config|
  config.root = Rails.root.join('tmp')
  config.cache_dir = 'carrierwave'
  
  config.fog_credentials = {
    :provider               => 'AWS',
    :aws_access_key_id      => ENV["AWS_ACCESS_KEY"],
    :aws_secret_access_key  => ENV["AWS_SECRET_KEY"]
  }
  config.fog_directory  = ENV["AWS_BUCKET"]
end

Change your storage setting in your app/uploaders/picture_uploader.rb file by uncommenting storage :fog and commenting out storage file

# app/uploaders/picture_uploader.rb

class PictureUploader < CarrierWave::Uploader::Base
  +storage :fog
  #storage :file
end

Lastly, create a new file called fog.rb in the lib directory and add a fog module to get the fog-aws gem working.

# lib/fog.rb

module Fog
  # :)
end

##Setting up Amazon S3

Now, let's setup our Amazon S3 bucket by first creating an account on aws.amazon.com.

Do the sign up procedure.

After you've finished that let's setup our user and bucket.

Create a user

Click on 'security and credentials' under your name in the top right of the navbar.

Click 'continue to security credentials' on the pop up.

Click on 'User' in the left side bar and then create a new user.

  1. Enter any username you want
  2. Leave the 'generate an access key for each user' checked
  3. And press create button

You should then see: Your 1 User(s) have been created successfully. This is the last time these User security credentials will be available for download.

===

At the botton of the page, press download your credentials. We will use these credentials later to connect our app with AWS.

Next click on 'close'. You should see the user you just created in your table. Click on that user.

Select the 'permissions' tab and click on 'attach policy'. Give the user administrative access (should be the first option).

Click on 'attach policy' to finish.

####Create an s3 bucket

Click on the “Services” button in the top left corner in the navbar and click 's3' from the dropdown menu. (all the way to the right)

Press 'create bucket'.

Enter a unique name for your bucket and the region you are in. Click 'create'. (You will need your bucket name later so remember it)

Currently your bucket is empty but later on they will be filled with the images that you uploaded from your app.

###More app setup

THIS IS IMPORTANT!

We need to setup Figaro gem to allow you to securely enter your AWS credentials in your rails application. It will hide your AWS credentials from the public.

Add figaro to your Gemfile

#Gemfile

gem 'fog-aws'
+ gem 'figaro', '~> 1.1', '>= 1.1.1'

On your command line bundle install and install figaro setup

$ bundle install

$ bundle exec figaro install

The bundle exec figaro install command will generate an application.yml file and add that file to your .gitignore. Any file declared in .gitignore will not be pushed up to github.

Check if the application.yml file was added to your .gitignore. It should have this code at the bottom of the .gitignore.

#.gitignore

# Ignore application configuration
/config/application.yml

To set up figaro for heroku, enter this command in the command line

$ figaro heroku:set -e production

Adding your AWS credentials to your app

Next it's time to open up your previously downloaded credentials from AWS and insert those credentials into your newly generated application.yml. You can delete all the commented code from the application.yml file first if you like. Then, paste in this code.

# config/application.yml

AWS_ACCESS_KEY: paste-aws-access-key-here
AWS_SECRET_KEY: paste-aws-secret-key-here
AWS_BUCKET: enter-your-bucket-name-here  #if you don't remember your bucket name, look back to the AWS site

Go through your git flow git status git add . git status git commit -m 'Add persist image feature' git status

Restart your server

Go to your localhost:3000/adventures/new and add an image to your app. Afterwards, check your s3 bucket to see if your image is inside. If it is, everything is working.


Adding your AWS credentials to heroku

For Heroku setup, go to your heroku app on www.heroku.com. In the settings of your app, click on 'reveal config vars' and add your aws credentials to the table one by one.

# Key             #Value
AWS_ACCESS_KEY | access-key-here
AWS_SECRET_KEY | aws-secret-key-here
AWS_BUCKET  | bucket-name-here

Finally, in the command line push your app to heroku

$ git push heroku master

and add a new picture. If it's all working, your picture should still be there a few hours later. Check your s3 bucket to see if your image is inside. If it is, everything is working



if you get an error with bin.rails

try deleting code in bin/rails and adding this code

# bin/rails

#!/usr/bin/env ruby
begin
  load File.expand_path("../spring", __FILE__)
rescue LoadError => e
  raise unless e.message.include?('spring')
end
APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment