Skip to content

How to: Make Carrierwave work on Heroku

tonytonyjan edited this page Feb 4, 2013 · 7 revisions

Heroku has a read-only filesystem, so uploads must be stored on S3 and cannot be cached in the public directory.

You can work around this by setting the cache_dir in your Uploader classes to the tmp directory:

class AvatarUploader < CarrierWave::Uploader::Base
  def cache_dir
    "#{Rails.root}/tmp/uploads"
  end
end

You can also set the cache_dir for all uploaders in an initializer:

CarrierWave.configure do |config|
  config.cache_dir = "#{Rails.root}/tmp/uploads"
end

Note that these techniques will disable the feature of making uploads work across form redisplays. Read the blog post Using Carrierwave caching on Heroku and review this example application for a technique that attempts to preserve this feature. In brief:

# config.ru
require ::File.expand_path('../config/environment',  __FILE__)
use Rack::Static, :urls => ['/carrierwave'], :root => 'tmp' # adding this line
run YourApplicationName::Application

# config/initializers/carrierwave.rb
CarrierWave.configure do |config|
  config.root = Rails.root.join('tmp') # adding these...
  config.cache_dir = 'carrierwave' # ...two lines

  config.fog_credentials = {
    :provider               => 'AWS',                        # required
    :aws_access_key_id      => 'key',                        # required
    :aws_secret_access_key  => 'secret',                     # required
    :region                 => 'eu-west-1',                  # optional, defaults to 'us-east-1'
    :host                   => 's3.example.com',             # optional, defaults to nil
    :endpoint               => 'https://s3.example.com:8080' # optional, defaults to nil
  }
  config.fog_directory  = 'directory'                             # required
  config.fog_public     = false                                   # optional, defaults to true
  config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}
end

Note that this technique may not work if you have more than one "dyno" on Heroku that has a different "tmp" directory. See the Heroku documentation for more detail.

Clone this wiki locally