Missing required arguments: aws_access_key_id, aws

2019-02-24 10:17发布

问题:

I am currently trying to run my test suite in terminal but i get the following error:

Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError)

I am using Carrierwave in my project and I am pretty sure that is what is causing the issue. here is my config/initializers/carrierwave.rb:

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',                       
    :aws_access_key_id      => ENV['S3_ACCESS_KEY_ID'],                        
    :aws_secret_access_key  => ENV['S3_SECRET_ACCESS_KEY']
  }
  config.fog_directory  = 'my_directory'                    

end

Here is my log:

News-MacBook-Pro:music-app yedidyaweiner$ bundle exec rspec
/Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/fog-core-1.27.2/lib/fog/core/service.rb:244:in `validate_options': Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError)
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/fog-core-1.27.2/lib/fog/core/service.rb:268:in `handle_settings'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/fog-core-1.27.2/lib/fog/core/service.rb:98:in `new'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/fog-core-1.27.2/lib/fog/storage.rb:25:in `new'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/carrierwave-0.10.0/lib/carrierwave/uploader/configuration.rb:83:in `eager_load_fog'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/carrierwave-0.10.0/lib/carrierwave/uploader/configuration.rb:96:in `fog_credentials='
    from /Users/yedidyaweiner/projects/music-app/config/initializers/carrierwave.rb:2:in `block in <top (required)>'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/carrierwave-0.10.0/lib/carrierwave/uploader/configuration.rb:118:in `configure'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/carrierwave-0.10.0/lib/carrierwave.rb:14:in `configure'
    from /Users/yedidyaweiner/projects/music-app/config/initializers/carrierwave.rb:1:in `<top (required)>'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `block in load'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in `load'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/railties-4.1.1/lib/rails/engine.rb:648:in `block in load_config_initializer'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/activesupport-4.1.1/lib/active_support/notifications.rb:161:in `instrument'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/railties-4.1.1/lib/rails/engine.rb:647:in `load_config_initializer'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/railties-4.1.1/lib/rails/engine.rb:612:in `block (2 levels) in <class:Engine>'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/railties-4.1.1/lib/rails/engine.rb:611:in `each'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/railties-4.1.1/lib/rails/engine.rb:611:in `block in <class:Engine>'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/railties-4.1.1/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/railties-4.1.1/lib/rails/initializable.rb:30:in `run'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/railties-4.1.1/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/yedidyaweiner/.rbenv/versions/2.1.2/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
    from /Users/yedidyaweiner/.rbenv/versions/2.1.2/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
    from /Users/yedidyaweiner/.rbenv/versions/2.1.2/lib/ruby/2.1.0/tsort.rb:418:in `block (2 levels) in each_strongly_connected_component_from'
    from /Users/yedidyaweiner/.rbenv/versions/2.1.2/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
    from /Users/yedidyaweiner/.rbenv/versions/2.1.2/lib/ruby/2.1.0/tsort.rb:417:in `block in each_strongly_connected_component_from'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/railties-4.1.1/lib/rails/initializable.rb:44:in `each'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/railties-4.1.1/lib/rails/initializable.rb:44:in `tsort_each_child'
    from /Users/yedidyaweiner/.rbenv/versions/2.1.2/lib/ruby/2.1.0/tsort.rb:411:in `call'
    from /Users/yedidyaweiner/.rbenv/versions/2.1.2/lib/ruby/2.1.0/tsort.rb:411:in `each_strongly_connected_component_from'
    from /Users/yedidyaweiner/.rbenv/versions/2.1.2/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
    from /Users/yedidyaweiner/.rbenv/versions/2.1.2/lib/ruby/2.1.0/tsort.rb:345:in `each'
    from /Users/yedidyaweiner/.rbenv/versions/2.1.2/lib/ruby/2.1.0/tsort.rb:345:in `call'
    from /Users/yedidyaweiner/.rbenv/versions/2.1.2/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
    from /Users/yedidyaweiner/.rbenv/versions/2.1.2/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
    from /Users/yedidyaweiner/.rbenv/versions/2.1.2/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/railties-4.1.1/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/railties-4.1.1/lib/rails/application.rb:288:in `initialize!'
    from /Users/yedidyaweiner/projects/music-app/config/environment.rb:5:in `<top (required)>'
    from /Users/yedidyaweiner/projects/music-app/spec/rails_helper.rb:4:in `require'
    from /Users/yedidyaweiner/projects/music-app/spec/rails_helper.rb:4:in `<top (required)>'
    from /Users/yedidyaweiner/projects/music-app/spec/controllers/comments_controller_spec.rb:1:in `require'
    from /Users/yedidyaweiner/projects/music-app/spec/controllers/comments_controller_spec.rb:1:in `<top (required)>'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `block in load_spec_files'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `each'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load_spec_files'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:97:in `setup'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:85:in `run'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:70:in `run'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:38:in `invoke'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.0.4/exe/rspec:4:in `<top (required)>'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/bin/rspec:23:in `load'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/bin/rspec:23:in `<main>'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/bin/ruby_executable_hooks:15:in `eval'
    from /Users/yedidyaweiner/.rvm/gems/ruby-2.1.3/bin/ruby_executable_hooks:15:in `<main>'

What do I need to do to get rid of this error and run my tests?

回答1:

You need to set the environment variables S3_ACCESS_KEY_ID and S3_SECRET_ACCESS_KEY so that they are available in Rails. You can do this in the terminal:

export S3_ACCESS_KEY_ID=<your access key id>
export S3_SECRET_ACCESS_KEY=<your secret access key>

Add those to your .bashrc or .bash_profile so they persist next time you start a new shell or use something like dotenv to make them specific to your project.



回答2:

Apart from the obvious solution, that is setting the proper ENV VARS, one other problem, that i ran into was spring. I had the ENV vars set correctly, but they were just not recognized by the carrierwave.rb initializer. Since spring is used to preload the application and also processes like rake and rspec it didn't take into account the newly created ENV VARS. Restarting spring fixed that.



回答3:

If you use figaro gem to manager your AWS password, you can try:
- figaro heroku:set -e production,
- then write heroku config, you will check the current config.
If everything is ok, there will list your AWS_ACCESS_KEY etc.