fog: Declaring only fog-aws in Gemfile: uninitialized constant CarrierWave::Storage::Fog

In an effort to load only the fog service gem we’re using, I’m declaring only fog-aws in the Gemfile:

gem fog-aws

However, on Rails console boot:

uninitialized constant CarrierWave::Storage::Fog
# backtrace line: ~/app/config/initializers/carrierwave.rb:4 (see below)

I’ve googled and stackoverflowed for examples of using specific service gems only. No luck.

Anyone know which gems to include to fix this? I would post in the Carrierwave project but this is only a problem when declaring fog-aws instead of fog.

Rails 4.1.8
Ruby 2.2
Fog 1.27
# initializer file
CarrierWave.configure do |config|
  if EnvInq.asset_store_on?
    config.cache_dir = "#{Rails.root}/tmp/"
    config.storage = :fog

    config.fog_credentials = {
      provider: 'AWS',
      aws_access_key_id: EnvInq.aws_access_key_id,
      aws_secret_access_key: EnvInq.aws_secret_access_key
    }

    config.asset_host = "https://#{EnvInq.asset_host}" if EnvInq.asset_host.present?
    config.fog_directory = EnvInq.aws_s3_bucket
    config.fog_public = true
    config.fog_attributes = { cache_control: 'max-age=315576000', expires: 1.year.from_now.httpdate }
  else
    config.storage = :file
  end
end

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 20 (7 by maintainers)

Most upvoted comments

Since require works with $LOAD_PATH and my Rails app has lib in its load path, I did the following:

$ touch lib/fog.rb

where the file contains the following:

module Fog
  # :D
end

and then in my CarrierWave initializer I did the following:

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

CarrierWave.configure do |config|
  …

the app then works with fog-aws.

Defining config.fog_credentials before config.storage = :fog worked for me, as referenced in this post #https://github.com/carrierwaveuploader/carrierwave/issues/2023

@kevin-bockman @geemus I was having the same issue. Moving the config.storage assignment after config.fog_* assignments fixed the problem for me.

This was problematic:

CarrierWave.configure do |config|

  if Rails.env.development? || Rails.env.test?
    config.storage = :file
  else
    config.storage = :fog
    config.fog_provider = 'fog/aws'
    config.fog_credentials = {
      provider: 'AWS',
      aws_access_key_id: ENV['S3_ACCESS_KEY_ID'],
      aws_secret_access_key: ENV['S3_SECRET_ACCESS_KEY'],
      region: ENV['S3_REGION'],
      endpoint: 'https://s3.amazonaws.com'
    }
    config.fog_directory = ENV['S3_BUCKET']
    config.fog_public = false
  end
end

This works:

CarrierWave.configure do |config|

  if Rails.env.development? || Rails.env.test?
    config.storage = :file
  else
    config.fog_provider = 'fog/aws'
    config.fog_credentials = {
      provider: 'AWS',
      aws_access_key_id: ENV['S3_ACCESS_KEY_ID'],
      aws_secret_access_key: ENV['S3_SECRET_ACCESS_KEY'],
      region: ENV['S3_REGION'],
      endpoint: 'https://s3.amazonaws.com'
    }
    config.fog_directory = ENV['S3_BUCKET']
    config.fog_public = false
    config.storage = :fog
  end
end

The above solution did not work. Seems this bug still exist.

@kburzota sorry to hear it took so long, but glad you found your solution. Thanks for sharing, hopefully that can help others that might run into this as well.

@evadne 's solution solved it for me. Thanks so much!