使用.env config

时间:2017-03-19 13:37:54

标签: ruby-on-rails ruby heroku amazon-s3

我有一个Rails 4应用程序部署到Heroku并与S3交谈。 S3的访问凭证存储在Heroku上的ENV变量中,本地我有一个.env文件。

当我通过heroku local -p 3000本地运行服务器时,一切正常 - 文件上传到S3就好了。

当我尝试通过heroku local:run rails console运行rails console时,我找不到魔法酱以避免发现密钥错误。

我错过了什么?

.ENV

S3_BUCKET_NAME=BUCKETNAME
AWS_ACCESS_KEY_ID=KEYID
AWS_SECRET_ACCESS_KEY=SECRETKEY
AWS_REGION=REGION

Procfile

web: bundle exec rails server -p $PORT

配置/环境/ development.rb

...
config.paperclip_defaults = {
  storage: :s3,
  s3_credentials: {
    bucket: ENV.fetch('S3_BUCKET_NAME'),
    access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
    secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
    s3_region: ENV.fetch('AWS_REGION'),
  }
}

错误堆栈:

heroku local:run rails console
[OKAY] Loaded ENV .env File as KEY=VALUE Format
/Users/username/Development/rails_application/config/environments/development.rb:48:in `fetch': key not found: "S3_BUCKET_NAME" (KeyError)
  from /Users/username/Development/rails_application/config/environments/development.rb:48:in `block in <top (required)>'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/railtie.rb:210:in `instance_eval'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/railtie.rb:210:in `configure'
  from /Users/username/Development/rails_application/config/environments/development.rb:1:in `<top (required)>'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `block in require'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/engine.rb:598:in `block (2 levels) in <class:Engine>'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/engine.rb:597:in `each'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/engine.rb:597:in `block in <class:Engine>'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/initializable.rb:30:in `instance_exec'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/initializable.rb:30:in `run'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/initializable.rb:55:in `block in run_initializers'
  from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:226:in `block in tsort_each'
  from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
  from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:420:in `block (2 levels) in each_strongly_connected_component_from'
  from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:429:in `each_strongly_connected_component_from'
  from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:419:in `block in each_strongly_connected_component_from'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/initializable.rb:44:in `each'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/initializable.rb:44:in `tsort_each_child'
  from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:413:in `call'
  from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:413:in `each_strongly_connected_component_from'
  from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:347:in `block in each_strongly_connected_component'
  from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `each'
  from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `call'
  from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:345:in `each_strongly_connected_component'
  from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:224:in `tsort_each'
  from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/tsort.rb:203:in `tsort_each'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/initializable.rb:54:in `run_initializers'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/application.rb:352:in `initialize!'
  from /Users/username/Development/rails_application/config/environment.rb:5:in `<top (required)>'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/spring-2.0.1/lib/spring/application.rb:92:in `require'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/spring-2.0.1/lib/spring/application.rb:92:in `preload'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/spring-2.0.1/lib/spring/application.rb:143:in `serve'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/spring-2.0.1/lib/spring/application.rb:131:in `block in run'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/spring-2.0.1/lib/spring/application.rb:125:in `loop'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/spring-2.0.1/lib/spring/application.rb:125:in `run'
  from /Users/username/.rvm/gems/ruby-2.2.1/gems/spring-2.0.1/lib/spring/application/boot.rb:19:in `<top (required)>'
  from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
  from /Users/username/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
  from -e:1:in `<main>'

2 个答案:

答案 0 :(得分:0)

来自dotenv README

# config/application.rb
Bundler.require(*Rails.groups)

Dotenv::Railtie.load

HOSTNAME = ENV['HOSTNAME']

另见.env not being loaded in test environment in Rails with rspec

答案 1 :(得分:0)

  1. 访问您的环境变量,如下所示:ENV [“AWS_KEY”]
  2. 确保您在heroku管理面板中设置了环境变量(最后一个标签,显示环境变量),不要将.env文件推送到存储库,因为这是一个危险的实践。