Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

https://github.com/jnicklas/carrierwave

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
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

Configure the model Model

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

Configure FOG storage

Configure carrierwave to use Amazon S3. The AWS credentials that's needed in the config can be found here: https://portal.aws.amazon.com/gp/aws/securityCredentials#access_credentials and visit https://console.aws.amazon.com/s3/home# 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|
      config.storage = :file
    end
  end
  
  # Use AWS storage if in production
  if Rails.env.production?
    CarrierWave.configure do |config|
      config.storage = :fog
    end
  end
  
  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       = 'https://assets.example.com'           # optional, defaults to nil
  #config.fog_public     = false                                  # optional, defaults to true
  config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}
end

Image quality

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

Convert image to PNG

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

Conditional processing / versions

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

def create_avatar?(img = nil)
    # conditions, return true / false
    # e.g. model.should_create_avatar?
end
@philip13

This comment has been minimized.

Copy link

philip13 commented Jun 27, 2017

service.rb:244:in `validate_options': Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError)

Can you help me, please?
I'm using carrierwave and use fog-amazon, the file storages is work but I don't know how solve the problem when I trying to run $ heroku local:run rails console

 $ heroku local:run rails console
[OKAY] Loaded ENV .env File as KEY=VALUE Format
/Users/developer/.rvm/gems/ruby-2.3.1/gems/fog-core-1.44.3/lib/fog/core/service.rb:244:in `validate_options': Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError)
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/fog-core-1.44.3/lib/fog/core/service.rb:268:in `handle_settings'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/fog-core-1.44.3/lib/fog/core/service.rb:98:in `new'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/fog-core-1.44.3/lib/fog/core/services_mixin.rb:16:in `new'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/fog-core-1.44.3/lib/fog/storage.rb:27:in `new'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/carrierwave-1.1.0/lib/carrierwave/uploader/configuration.rb:124:in `eager_load_fog'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/carrierwave-1.1.0/lib/carrierwave/uploader/configuration.rb:137:in `fog_credentials='
	from /Users/developer/projects/exhibittrader/config/initializers/carrierwave.rb:3:in `block in <top (required)>'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/carrierwave-1.1.0/lib/carrierwave/uploader/configuration.rb:159:in `configure'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/carrierwave-1.1.0/lib/carrierwave.rb:14:in `configure'
	from /Users/developer/projects/exhibittrader/config/initializers/carrierwave.rb:1:in `<top (required)>'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in `load'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in `block in load'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in `load_dependency'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in `load'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/railties-5.0.2/lib/rails/engine.rb:648:in `block in load_config_initializer'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.2/lib/active_support/notifications.rb:166:in `instrument'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/railties-5.0.2/lib/rails/engine.rb:647:in `load_config_initializer'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/railties-5.0.2/lib/rails/engine.rb:612:in `block (2 levels) in <class:Engine>'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/railties-5.0.2/lib/rails/engine.rb:611:in `each'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/railties-5.0.2/lib/rails/engine.rb:611:in `block in <class:Engine>'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/railties-5.0.2/lib/rails/initializable.rb:30:in `instance_exec'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/railties-5.0.2/lib/rails/initializable.rb:30:in `run'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/railties-5.0.2/lib/rails/initializable.rb:55:in `block in run_initializers'
	from /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
	from /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
	from /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from'
	from /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
	from /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:421:in `block in each_strongly_connected_component_from'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/railties-5.0.2/lib/rails/initializable.rb:44:in `each'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/railties-5.0.2/lib/rails/initializable.rb:44:in `tsort_each_child'
	from /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:415:in `call'
	from /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:415:in `each_strongly_connected_component_from'
	from /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
	from /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each'
	from /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `call'
	from /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
	from /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
	from /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/railties-5.0.2/lib/rails/initializable.rb:54:in `run_initializers'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/railties-5.0.2/lib/rails/application.rb:352:in `initialize!'
	from /Users/developer/projects/exhibittrader/config/environment.rb:6:in `<top (required)>'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in `require'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in `block in require'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:259:in `load_dependency'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:293:in `require'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/spring-2.0.1/lib/spring/application.rb:92:in `preload'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/spring-2.0.1/lib/spring/application.rb:143:in `serve'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/spring-2.0.1/lib/spring/application.rb:131:in `block in run'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/spring-2.0.1/lib/spring/application.rb:125:in `loop'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/spring-2.0.1/lib/spring/application.rb:125:in `run'
	from /Users/developer/.rvm/gems/ruby-2.3.1/gems/spring-2.0.1/lib/spring/application/boot.rb:19:in `<top (required)>'
	from /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from /Users/developer/.rvm/rubies/ruby-2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
	from -e:1:in `<main>'
@philip13

This comment has been minimized.

Copy link

philip13 commented Jun 27, 2017

I solved the error, the problem was because I added the config.fog_porvider = 'fog/amazon', check the issue of resolve the problem

@bdhiru

This comment has been minimized.

Copy link

bdhiru commented Sep 22, 2017

please tell

<%- model_class = Product -%>

<%=t '.title', :default => model_class.model_name.human.pluralize.titleize %>

  <th><%= model_class.human_attribute_name(:id) %></th>
  <th><%= model_class.human_attribute_name(:name) %></th>
  <th><%= model_class.human_attribute_name(:price) %></th>
  <th><%= model_class.human_attribute_name(:ptype) %></th>
  <th><%= model_class.human_attribute_name(:size) %></th>
  <th><%= model_class.human_attribute_name(:color) %></th>
  <th><%= model_class.human_attribute_name(:product_category_id) %></th>
  <th><%= model_class.human_attribute_name(:created_at) %></th>
  <th><%=t '.actions', :default => t("helpers.actions") %></th>
</tr>
<% @products.each do |product| %> <% end %>
<%= model_class.human_attribute_name(:image) %>
<%= link_to product.id, product_path(product) %> <%= product.name %> <%= product.price %> <%= product.ptype %> <%= product.size %> <%= product.color %> <% @product.images.each do |image|%> <%=image_tag image.image.path %> <% end%> <%= product.product_category_id %> <%=l product.created_at %> <%= link_to t('.show', :default => t("helpers.links.show")), admins_product_path(product), :class => 'btn btn-default btn-xs' %> <%= link_to t('.edit', :default => t("helpers.links.edit")), edit_admins_product_path(product), :class => 'btn btn-default btn-xs' %> <%= link_to t('.destroy', :default => t("helpers.links.destroy")), admins_product_path(product), :method => :delete, :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, :class => 'btn btn-xs btn-danger' %>

<%= link_to t('.new', :default => t("helpers.links.new")),
new_admins_product_path,
:class => 'btn btn-primary' %>

no method
error found for image

<%= product.color %> <% @product.images.each do |image|%> <%=image_tag image.image.path %> <% end%>
@hiro2016

This comment has been minimized.

Copy link

hiro2016 commented Dec 8, 2017

Thanks for a very useful guide.
I faced some issue deploying my app on ec2, however:
NameError: uninitialized constant CarrierWave::Storage::Fog.
Moving the section below to the last solved it.

  if Rails.env.production?
    CarrierWave.configure do |config|
      config.storage = :fog
    end
  end
@fagianijunior

This comment has been minimized.

Copy link

fagianijunior commented Mar 7, 2018

How to make a link to download this file? in my case a pdf or jpg files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.