2015-02-09 79 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) 

希望它能帮助!

相关问题