-
Notifications
You must be signed in to change notification settings - Fork 45
Setup Carrierwave & Rails for AWS
Abraham edited this page Sep 23, 2016
·
2 revisions
This guide will show how to configure a Rails application that uses carrierwave to handle file uploads along with S3 from AWS.
- You already have an AWS active account
On your Gemfile
add:
gem "carrierwave"
gem "mini_magick"
gem "fog", "1.25"
Then run the bundle install
command to install the dependencies.
First create a file under config/initializers
called carrierwave.rb
. The name is not relevant, it is just to make sense. The content of that file should look like:
if Rails.env.production?
CarrierWave.configure do |config|
config.storage = :fog
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'], # required
:aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'], # required
}
config.fog_directory = ENV['AWS_BUCKET_NAME'] # required
config.fog_public = true # optional, defaults to true
config.root = Rails.root.join('tmp')
config.cache_dir = 'files'
config.permissions = 0777
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
end
else
CarrierWave.configure do |config|
config.storage :file
end
end
The script above will use our local computer to store the files when working on development, but in production every file will be uploaded to S3.