Skip to content

Instantly share code, notes, and snippets.

Forked from marcusmalmberg/
Created June 2, 2017 08:37
Show Gist options
  • Save irridescentrambler/0f0fbb454c770887268f4fe23c6af10a to your computer and use it in GitHub Desktop.
Save irridescentrambler/0f0fbb454c770887268f4fe23c6af10a to your computer and use it in GitHub Desktop.
Guide to setup CarrierWave which will upload a file to Amazon S3 in production environment and use local storage in development and test

CarrierWave home

This example will create an uploader that will upload a file stored in a model Model. The file will be stored locally in development and test environment and will use Amazon S3 in production.

CarrierWave installation

First add the gems.

# Gemfile
gem 'carrierwave'
gem 'fog'

Install gems.

bundle install

CarrierWave create uploader

Generate uploader

rails generate uploader Model

Create model Model

rails generate model model file:string
rake db:migrate

Update the ModelUploader

# app/uploaders/model_uploader.rb 
class ModelUploader < CarrierWave::Uploader::Base

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir

Configure the model Model

# app/models/model.rb 
class Model < ActiveRecord::Base
  mount_uploader :file, ModelUploader
  attr_accessible :file

Configure FOG storage

Configure carrierwave to use Amazon S3. The AWS credentials that's needed in the config can be found here: and visit to setup a bucket.

# config/initializers/carrierwave.rb
# This file is not created by default so you might have to create it yourself.

CarrierWave.configure do |config|
  # Use local storage if in development or test
  if Rails.env.development? || Rails.env.test?
    CarrierWave.configure do |config| = :file
  # Use AWS storage if in production
  if Rails.env.production?
    CarrierWave.configure do |config| = :fog
  config.fog_credentials = {
    :provider               => 'AWS',                             # required
    :aws_access_key_id      => '<your key goes here>',            # required
    :aws_secret_access_key  => '<your secret key goes here>',     # required
    :region                 => 'eu-west-1'                        # optional, defaults to 'us-east-1'
  config.fog_directory  = '<bucket name goes here>'               # required
  #config.fog_host       = ''           # optional, defaults to nil
  #config.fog_public     = false                                  # optional, defaults to true
  config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}

Image quality

# config/initializers/carrierwave.rb
module CarrierWave
  module MiniMagick
    def quality(percentage)
      manipulate! do |img|
        img = yield(img) if block_given?

Convert image to PNG

# app/uploaders/my_uploader.rb
process :convert => 'png'
def filename
    super.chomp(File.extname(super)) + '.png' if original_filename

Conditional processing / versions

# app/uploaders/my_uploader.rb
version :avatar, if: :create_avatar? do
    process :resize_to_limit => [200, 200]

def create_avatar?(img = nil)
    # conditions, return true / false
    # e.g. model.should_create_avatar?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment