Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
lukewendling opened this Issue Feb 4, 2015 · 20 comments

Comments

Projects
None yet
@lukewendling
Copy link

lukewendling commented Feb 4, 2015

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
@plribeiro3000

This comment has been minimized.

Copy link
Member

plribeiro3000 commented Feb 4, 2015

@lukewendling Which version of carrierwave are you using?

@plribeiro3000

This comment has been minimized.

Copy link
Member

plribeiro3000 commented Feb 4, 2015

I did a deeper dig and found this file It is requiring fog and rescuing LoadError so if you have fog-aws it won't load aws at all.

So for now you need to use fog instead of just fog-aws. In the future we are gotta create service based gems like fog-storage that will help this kind of issue.

Thanks! Hope this help you understand the issue better!

@lukewendling

This comment has been minimized.

Copy link
Author

lukewendling commented Feb 4, 2015

@plribeiro3000 Thanks for the explanation. I'll watch for updates and continue to scan the Readme for the new feature.

@geemus

This comment has been minimized.

Copy link
Member

geemus commented Feb 5, 2015

You might also be able to get it to work by manually including fog-aws prior to carrierwave (could be worth a quick try anyway). That said, it may ultimately be that we would need to tweak carrierwave to better support this case.

@evadne

This comment has been minimized.

Copy link

evadne commented Sep 24, 2015

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.

@aashish

This comment has been minimized.

Copy link

aashish commented Aug 23, 2016

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

@dyz2102

This comment has been minimized.

Copy link

dyz2102 commented Dec 13, 2016

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

@tannerjt

This comment has been minimized.

Copy link

tannerjt commented Jan 4, 2017

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

@suilu

This comment has been minimized.

Copy link

suilu commented Feb 19, 2017

@evadne Thank you very much.I solved my issue!

@RaVbaker

This comment has been minimized.

Copy link

RaVbaker commented Mar 1, 2017

There is cleaner solution than making custom module Fog in your app under file like lib/fog.rb. Try doing: config.fog_provider = "fog/aws" in line before fog_credentials.

Like:

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

    config.fog_provider = "fog/aws"
    config.fog_credentials = {
      provider: 'AWS',
      aws_access_key_id: EnvInq.aws_access_key_id,
      aws_secret_access_key: EnvInq.aws_secret_access_key
    }

  # ...
end

mazgi added a commit to kontestapp/Kontest that referenced this issue May 14, 2017

@kevinmullican

This comment has been minimized.

Copy link

kevinmullican commented Jun 12, 2017

I am hitting this same issue (the above referenced error uninitialized constant CarrierWave::Storage::Fog).

ruby 2.3.1p112
Rails 4.2.8
fog (1.40.0)
fog-aws (>= 0.6.0)
carrierwave (1.1.0)

I have tried moving config.storage = :fog after the credentials, but that results in an error complaining of undefined aws_access_key_id and undefined aws_secret_access_key.

Here is the error with config.storage = :fog after credentials:

/home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/fog-core-1.44.1/lib/fog/core/service.rb:244:in `validate_options': Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError)
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/fog-core-1.44.1/lib/fog/core/service.rb:268:in `handle_settings'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/fog-core-1.44.1/lib/fog/core/service.rb:98:in `new'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/fog-core-1.44.1/lib/fog/core/services_mixin.rb:16:in `new'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/fog-core-1.44.1/lib/fog/storage.rb:27:in `new'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/uploader/configuration.rb:124:in `eager_load_fog'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/uploader/configuration.rb:137:in `fog_credentials='
	from /home/pelican/src/newshop/config/initializers/carrierwave.rb:5:in `block in <top (required)>'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/uploader/configuration.rb:159:in `configure'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/carrierwave-1.1.0/lib/carrierwave.rb:14:in `configure'
	from /home/pelican/src/newshop/config/initializers/carrierwave.rb:1:in `<top (required)>'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:268:in `load'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:268:in `block in load'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:240:in `load_dependency'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:268:in `load'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/engine.rb:652:in `block in load_config_initializer'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/notifications.rb:166:in `instrument'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/engine.rb:651:in `load_config_initializer'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/engine.rb:616:in `block (2 levels) in <class:Engine>'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/engine.rb:615:in `each'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/engine.rb:615:in `block in <class:Engine>'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/initializable.rb:30:in `instance_exec'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/initializable.rb:30:in `run'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/initializable.rb:55:in `block in run_initializers'
	from /usr/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
	from /usr/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
	from /usr/lib/ruby/2.3.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from'
	from /usr/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
	from /usr/lib/ruby/2.3.0/tsort.rb:421:in `block in each_strongly_connected_component_from'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/initializable.rb:44:in `each'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/initializable.rb:44:in `tsort_each_child'
	from /usr/lib/ruby/2.3.0/tsort.rb:415:in `call'
	from /usr/lib/ruby/2.3.0/tsort.rb:415:in `each_strongly_connected_component_from'
	from /usr/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
	from /usr/lib/ruby/2.3.0/tsort.rb:347:in `each'
	from /usr/lib/ruby/2.3.0/tsort.rb:347:in `call'
	from /usr/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
	from /usr/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
	from /usr/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/initializable.rb:54:in `run_initializers'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/application.rb:352:in `initialize!'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/railtie.rb:194:in `public_send'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/railtie.rb:194:in `method_missing'
	from /home/pelican/src/newshop/config/environment.rb:5:in `<top (required)>'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:274:in `require'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:274:in `block in require'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:240:in `load_dependency'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:274:in `require'
	from /home/pelican/src/newshop/config.ru:3:in `block in <main>'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/builder.rb:55:in `instance_eval'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/builder.rb:55:in `initialize'
	from /home/pelican/src/newshop/config.ru:in `new'
	from /home/pelican/src/newshop/config.ru:in `<main>'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/builder.rb:49:in `eval'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/builder.rb:49:in `new_from_string'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/builder.rb:40:in `parse_file'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/server.rb:299:in `build_app_and_options_from_config'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/server.rb:208:in `app'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/commands/server.rb:61:in `app'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/rack-1.6.5/lib/rack/server.rb:336:in `wrapped_app'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/commands/server.rb:139:in `log_to_stdout'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/commands/server.rb:78:in `start'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:80:in `block in server'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:75:in `tap'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:75:in `server'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/railties-4.2.8/lib/rails/commands.rb:17:in `<top (required)>'
	from /home/pelican/src/newshop/bin/rails:8:in `require'
	from /home/pelican/src/newshop/bin/rails:8:in `<top (required)>'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/spring-2.0.1/lib/spring/client/rails.rb:28:in `load'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/spring-2.0.1/lib/spring/client/rails.rb:28:in `call'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/spring-2.0.1/lib/spring/client/command.rb:7:in `call'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/spring-2.0.1/lib/spring/client.rb:30:in `run'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/spring-2.0.1/bin/spring:49:in `<top (required)>'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/spring-2.0.1/lib/spring/binstub.rb:31:in `load'
	from /home/pelican/src/newshop/vendor/bundle/ruby/2.3.0/gems/spring-2.0.1/lib/spring/binstub.rb:31:in `<top (required)>'
	from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68:in `require'
	from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68:in `require'
	from /home/pelican/src/newshop/bin/spring:16:in `<top (required)>'
	from /home/pelican/src/newshop/bin/rails:3:in `load'
	from /home/pelican/src/newshop/bin/rails:3:in `<top (required)>'
	from -e:1:in `load'
	from -e:1:in `<main>'

here is the config:

CarrierWave.configure do |config|
  #config.storage = :fog

  #config.fog_provider = 'fog/aws'
  config.fog_credentials = {
    :provider               => 'AWS',
    :aws_access_key_id      => ENV['AWS_ACCESS_KEY_ID'],
    :aws_secret_access_key  => ENV['AWS_SECRET_ACCESS_KEY'],
  }
  config.storage = :fog
  config.fog_provider = 'fog/aws'
  config.fog_directory  = ENV['S3_BUCKET']
end
@geemus

This comment has been minimized.

Copy link
Member

geemus commented Jun 12, 2017

@kevinmullican Are you sure the ENV contains what you expect? Otherwise this looks reasonable, so that would be the first thing that I would double check.

@kevinmullican

This comment has been minimized.

Copy link

kevinmullican commented Jun 12, 2017

pretty sure ENV does contain what's expected

@geemus

This comment has been minimized.

Copy link
Member

geemus commented Jun 12, 2017

I'm not sure beyond that, it may make sense to check-in with carrierwave on this as it sounds like something on that side (especially if changing order of things is so impactful).

@kevinmullican

This comment has been minimized.

Copy link

kevinmullican commented Jun 12, 2017

@geemus thanks for the response, I'll look more deeply...

@kpheasey

This comment has been minimized.

Copy link

kpheasey commented Jul 7, 2017

@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
@geemus

This comment has been minimized.

Copy link
Member

geemus commented Jul 7, 2017

@kpheasey ah ha, thanks for the tip!

@kburzota

This comment has been minimized.

Copy link

kburzota commented Jan 9, 2018

thanks @kpheasey! Moving config.storage after the config.fogs resolved my issue as well. Hours of troubleshooting but it's finally running!

@geemus

This comment has been minimized.

Copy link
Member

geemus commented Jan 10, 2018

@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.

@fagiani

This comment has been minimized.

Copy link

fagiani commented Aug 19, 2018

@kevinmullican I had the same issue you mentioned on #3429 (comment) and somehow ENV's set on application.yml won't be available at that very moment. So I've repaced ENV's with Rails.application.secrets and placed them on the secrets.yml instead.

Hope that helps someone in the future!

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.