2015-02-09 80 views
1

我曾在〜/ .bashrc保存一些ENV的,請重新打開該文件,我可以有看到他們:然而如何讓我的Rails應用程序使用我的〜/ .bashrc保存的ENV VARS?

export SECRET_KEY_BASE="secretkeyhere" 
export S3_SECRET_KEY="anotherhere" 
export S3_ACCESS_KEY="andhere" 
export DEVISE_KEY="and again" 

,當我告訴我的Rails應用程序使用它們,例如S3憑據在我與ENV["S3_ACCESS_KEY"]模型它似乎沒有使用它們,給我的錯誤:

Missing Credentials. Unable to find AWS credentials. You can configure your AWS credentials a few different ways: * Call AWS.config with :access_key_id and :secret_access_key * Export AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to ENV * On EC2 you can run instances with an IAM instance profile and credentials will be auto loaded from the instance metadata service on those instances. * Call AWS.config with :credential_provider. A credential provider should either include AWS::Core::CredentialProviders::Provider or respond to the same public methods. = Ruby on Rails In a Ruby on Rails application you may also specify your credentials in the following ways: * Via a config initializer script using any of the methods mentioned above (e.g. RAILS_ROOT/config/initializers/aws-sdk.rb). * Via a yaml configuration file located at RAILS_ROOT/config/aws.yml. This file should be formated like the default RAILS_ROOT/config/database.yml file. 

當殼我運行echo $DEVISE_KEY並返回不過是一個空行,如果我跑source ~/.bashrc然後echo $DEVISE_KEY它返回鍵?!?因此,它似乎在某處,但不能通過rails和/或正確的會話/環境/程序訪問。作爲UNIX的初級,Rails以及更多,我現在對這裏發生的事情感到不知所措。請你能幫我理解我在這裏做錯了什麼。謝謝。

+0

您是否嘗試過採購您的bashrc文件? – Joel 2015-02-09 22:33:15

+0

我試着在bash窗口中運行'source〜/ .bashrc',然後'echo $ DEVISE_KEY'會返回密鑰。然而,Rails仍然在PostsController#create'錯誤中返回相同的'AWS :: Errors :: MissingCredentialsError? – jbk 2015-02-10 11:05:50

回答

1

作爲處理應用程序中環境變量的替代方法,我建議你看看dotenv gem

而不必應付的〜/ .bashrc文件(在你的情況肯定是不正確載入),你可以有一個.env文件在本地環境中所定義的變量,並且將被加載到ENV當環境被引導時。您不會將您的.env文件提交給您的版本控制,並將其保留在本地環境中。

在你的情況,你會增加你的變量.env

SECRET_KEY_BASE="secretkeyhere" 
S3_SECRET_KEY="anotherhere" 
S3_ACCESS_KEY="andhere" 
DEVISE_KEY="and again" 

,然後就打電話給他們在AWS的配置文件是這樣的:

config.secret_key = ENV['S3_SECRET_KEY'] 

(or whatever the config method is) 

希望它能幫助!

相關問題