2

I deploy my rails app onto Heroku, and I've set aws access keys on the server as environment variables. However, to test my application in development environment, I need to initialize them somewhere on my local machine. So I decided to the following.

/config/initailizers/init_aws_locally.rb

ENV['AWS_ACCESS_KEY_ID'] = 'my key'
ENV['AWS_SECRET_ACCESS_KEY'] = 'my secret key'

This file is added in .gitignore

However, when I upload in development environment, I get this error message:

Missing required arguments: aws_access_key_id, aws_secret_access_key

I think somehow I overlooked a simple step to include my aws keys in my development environment. But I'm not sure why the reason for the error when I already initialized the keys.

For your reference, I'm using carrierwave, S3, and Fog.

config/initializers/fog.rb

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',                        # required
    :aws_access_key_id      => ENV['AWS_ACCESS_KEY_ID'],       # required
    :aws_secret_access_key  => ENV['AWS_SECRET_ACCESS_KEY'], # required
    :region                 => 'us-east-1',                  # optional, defaults to 'us-east-1'
  }
  config.fog_directory  = 'd'                         # required
  config.fog_public     = true                                # optional, defaults to true
end

Thank you. I appreciate your help!

4

3 に答える 3

3

Your initializers will be run in alphabetical order. See the docs:

If you have any ordering dependency in your initializers, you can control the load order by naming. For example, 01_critical.rb will be loaded before 02_normal.rb.

The problem you're experiencing is that your fog.rb initializer is running before your init_aws_locally.rb one (because f comes before i). So ENV['AWS_ACCESS_KEY_ID'] has not been defined (yet) when you set fog_credentials.

于 2013-01-21T06:05:00.130 に答える
1

コードにクレデンシャルを入れることは避けたいと思います。それはとてもひどい考えであり、Herokuは正しい考えを持っています。したがって、私が行うことは、RVMを使用して、ファイル.rvmrcをプロジェクトフォルダーに配置することです。.rvmrcも.gitignoreに入れました。

次に、.rvmrcを編集して

AWS_ACCESS_KEY_ID="BLAH"をエクスポートします

などなど。このディレクトリに「cd」すると、RVMによってそのプロジェクト用にenvが設定されます。RVMを使用していない場合は、他の方法があります。

レールs

.rvmrcスクリプトに入力したすべての環境変数が設定されます。イニシャライザーや開発の必要はなく、ソース管理の対象外になっているyaml構成ファイルのみです。私の経験から、これは最も簡単な解決策です。

于 2013-01-21T05:10:56.747 に答える
0

I went to my shell and typed:

$ echo $AWS_SECRET_ACCESS_KEY

and it came back blank. It turns out I recently moved to a new virtual machine and forgot to add this to the .bashrc file. It's worth checking the shell environment just in case. Once I added those two lines to my .bashrc, everything was happy again.

于 2013-09-12T06:14:07.977 に答える