缺少必需的参数:服务器中的aws_access_key_id,aws_secret_access_key

时间:2017-01-15 17:13:59

标签: ruby-on-rails server carrierwave fog

每当我尝试启动服务器时,我都会收到此错误validate_options': Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError)我似乎无法解决问题!我几乎可以肯定,这个问题导致Carrierwave和雾宝石使用亚马逊s3。但我不确定问题究竟在哪个文件夹中!

这是我的初始化程序/ carrierwave.rb:

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider => 'AWS',                        # required
    :aws_access_key_id  =>   ENV["S3_ACCESS_KEY"],                        # required
    :aws_secret_access_key => ENV["S3_SECRET_KEY"]                      # required
  }
  config.fog_directory  = ENV["S3_BUCKET"]                          # required
end

这是我的日志:

=> Booting WEBrick
=> Rails 4.2.5 application starting in development on http://0.0.0.0:8080
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/usr/local/rvm/gems/ruby-2.3.0/gems/fog-core-1.43.0/lib/fog/core/service.rb:244:in `validate_options': Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError)
        from /usr/local/rvm/gems/ruby-2.3.0/gems/fog-core-1.43.0/lib/fog/core/service.rb:268:in `handle_settings'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/fog-core-1.43.0/lib/fog/core/service.rb:98:in `new'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/fog-core-1.43.0/lib/fog/core/services_mixin.rb:16:in `new'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/fog-core-1.43.0/lib/fog/storage.rb:27:in `new'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/carrierwave-1.0.0/lib/carrierwave/uploader/configuration.rb:123:in `eager_load_fog'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/carrierwave-1.0.0/lib/carrierwave/uploader/configuration.rb:136:in `fog_credentials='
        from /home/ubuntu/workspace/config/initializers/carrierwave.rb:2:in `block in <top (required)>'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/carrierwave-1.0.0/lib/carrierwave/uploader/configuration.rb:158:in `configure'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/carrierwave-1.0.0/lib/carrierwave.rb:14:in `configure'
        from /home/ubuntu/workspace/config/initializers/carrierwave.rb:1:in `<top (required)>'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:652:in `block in load_config_initializer'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/notifications.rb:166:in `instrument'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:651:in `load_config_initializer'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:616:in `block (2 levels) in <class:Engine>'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:615:in `each'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:615:in `block in <class:Engine>'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `instance_exec'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `run'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/initializable.rb:55:in `block in run_initializers'
        from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
        from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
        from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from'
        from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
        from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:421:in `block in each_strongly_connected_component_from'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/initializable.rb:44:in `each'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/initializable.rb:44:in `tsort_each_child'
        from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:415:in `call'
        from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:415:in `each_strongly_connected_component_from'
        from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
        from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:347:in `each'
        from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:347:in `call'
        from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
        from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
        from /usr/local/rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/initializable.rb:54:in `run_initializers'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/application.rb:352:in `initialize!'
        from /home/ubuntu/workspace/config/environment.rb:5:in `<top (required)>'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
        from /home/ubuntu/workspace/config.ru:3:in `block in <main>'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.5/lib/rack/builder.rb:55:in `instance_eval'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.5/lib/rack/builder.rb:55:in `initialize'
        from /home/ubuntu/workspace/config.ru:in `new'
        from /home/ubuntu/workspace/config.ru:in `<main>'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.5/lib/rack/builder.rb:49:in `eval'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.5/lib/rack/builder.rb:49:in `new_from_string'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.5/lib/rack/builder.rb:40:in `parse_file'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.5/lib/rack/server.rb:299:in `build_app_and_options_from_config'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.5/lib/rack/server.rb:208:in `app'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands/server.rb:61:in `app'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.5/lib/rack/server.rb:336:in `wrapped_app'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands/server.rb:139:in `log_to_stdout'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands/server.rb:78:in `start'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:80:in `block in server'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:75:in `tap'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:75:in `server'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
        from /home/ubuntu/workspace/bin/rails:9:in `require'
        from /home/ubuntu/workspace/bin/rails:9:in `<top (required)>'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.0/lib/spring/client/rails.rb:28:in `load'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.0/lib/spring/client/rails.rb:28:in `call'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.0/lib/spring/client/command.rb:7:in `call'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.0/lib/spring/client.rb:30:in `run'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.0/bin/spring:49:in `<top (required)>'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.0/lib/spring/binstub.rb:31:in `load'
        from /usr/local/rvm/gems/ruby-2.3.0/gems/spring-2.0.0/lib/spring/binstub.rb:31:in `<top (required)>'
        from /home/ubuntu/workspace/bin/spring:13:in `require'
        from /home/ubuntu/workspace/bin/spring:13:in `<top (required)>'
        from bin/rails:3:in `load'
        from bin/rails:3:in `<main>'

3 个答案:

答案 0 :(得分:1)

<强>已更新

在您的yml文件(例如secrets.yml)中声明您的AWS凭据,如下所示:

AWS_ACCESS_KEY_ID: XXXXXXXXXXXXXXXXXXXX
AWS_SECRET_ACCESS_KEY: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ARTIFACTS_REGION: eu-central-1
ARTIFACTS_BUCKET: some_bucket

请勿忘记将包含您的凭据的文件添加到.gitignore

然后在carrierwave配置文件(carrierwave.rb)中使用您的凭据,如下所示:

config.fog_credentials = {
  provider:              'AWS',            
  aws_access_key_id:     ENV['AWS_ACCESS_KEY_ID'],          
  aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],             
  region:                ENV['ARTIFACTS_REGION']            
}

<强>已更新

要更轻松地管理环境变量,请尝试Figaro gem。也许它会暴露你的实际问题而你会解决它。

答案 1 :(得分:1)

通常这是由未在env中设置的值或意外拼写错误造成的。

有几种方法可以解决这个问题,但在开发过程中,最直接的方法是使用导出,如下所示:

export S3_ACCESS_KEY=[...]

如果用你的值填充[...]部分(并重复三个不同的变量),它应该能够找到所有内容。

你也可以使用echo来检查这些值的当前值:

echo $S3_ACCESS_KEY

如果您正确设置了值,则应将值打印到终端。如果未设置该值,则只会打印一个空行(如果任何值都有空行,则会导致您看到的错误。)

答案 2 :(得分:1)

我的错误

我有一个完全相同的错误:validate_options': Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError),但是我的问题是我创建了名称为mycompany.bucket的存储桶,这是不允许的,这给我带来了很多麻烦。您不能在存储桶名称中使用点。有关更多信息,请访问:

桶限制和限制-Amazon Simple Storage Service https://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html


建议

现在,我个人避免使用secrets.yml方法。我建议将雾用于生产,将文件存储用于开发。例如:

photo_uploader.rb

class PhotoUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  process resize_to_fill: [100, 100]
  # Choose what kind of storage to use for this uploader:
  if Rails.env.production?
    storage :fog
  else
    storage :file
  end

  # 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

  # Add a white list of extensions which are allowed to be uploaded.
  def extension_whitelist
    %w[jpg jpeg gif png]
  end
end

宝石文件

group :production do
  gem 'fog', '1.42'
end

carrierwave.rb

if Rails.env.production?

  CarrierWave.configure do |config|
    config.fog_provider = 'fog/aws'                        
    config.fog_credentials = {
      provider:              'AWS',                        
      aws_access_key_id:     ENV["S3_ACCESS_KEY"],        
      aws_secret_access_key: ENV["S3_SECRET_KEY"],
    }
    config.fog_directory  = ENV["S3_BUCKET"]              
  end

end

环境变量

如果您仍然想管理环境变量,也许可以派上用场

临时:

export S3_ACCESS_KEY=[...]
export S3_SECRET_KEY=[...]
export S3_BUCKET=[...]

设置它们后,您可以打印它们以检查它们是否被设置:

printenv

您还可以取消设置变量:

unset S3_ACCESS_KEY
unset S3_SECRET_KEY
unset S3_BUCKET

永久:

Edit the ~/.bashrc to set the environmental variables permanently.

希望这对您有帮助,祝您度过愉快的一天!