在rake测试中缺少必需的参数:aws_access_key_id,aws_secret_access_key

时间:2015-04-30 16:32:36

标签: ruby-on-rails ruby heroku amazon-web-services railstutorial.org

我正在做hartle教程的第11章。 当我运行heroku run rake db:migrate时出现此错误:

Missing required arguments: aws_access_key_id, aws_secret_access_key

我用enter link description here中的答案解决了它并成功迁移。但是现在我运行

bundle exec rake test

它给了我:

rake aborted!
ArgumentError: Missing required arguments: aws_access_key_id, aws_secret_access_key

这是我的carrierwave文件:

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',
    :aws_access_key_id      => ENV['S3_KEY'],
    :aws_secret_access_key  => ENV['S3_SECRET'],
    :region                 => ENV['S3_REGION'],
    :endpoint               => ENV['S3_ENDPOINT']
  }

  if Rails.env.test? || Rails.env.development?
    config.storage = :file
    config.enable_processing = false
    config.root = "#{Rails.root}/tmp/uploads/#{DateTime.now.to_f}.#{rand(999)}.#{rand(999)}"
  else
    config.storage = :fog
  end

  config.cache_dir = "#{Rails.root}/tmp/uploads/#{DateTime.now.to_f}.#{rand(999)}.#{rand(999)}"
  config.fog_directory  = ENV['S3_BUCKET_NAME']
  config.fog_public     = false
  config.fog_attributes = {}
end

我在enter link description here中测试了答案,但它对我没有用。

1 个答案:

答案 0 :(得分:0)

我会在测试助手中设置这些:

ENV['S3_KEY'] = 'S3_KEY'
ENV['S3_SECRET'] = 'S3_SECRET'
ENV['S3_REGION'] = 'S3_REGION'
ENV['S3_ENDPOINT'] = 'S3_ENDPOINT'

或者,您可以

if ! Rails.env.test?
  config.fog_credentials = {
    :provider               => 'AWS',
    :aws_access_key_id      => ENV['S3_KEY'],
    :aws_secret_access_key  => ENV['S3_SECRET'],
    :region                 => ENV['S3_REGION'],
    :endpoint               => ENV['S3_ENDPOINT']
  }
end

但我认为第一个更好,因为它不会用测试代码污染您的生产代码。