Ruby on rails 如何让Rails应用程序使用~/.bashrc保存的环境变量?

Ruby on rails 如何让Rails应用程序使用~/.bashrc保存的环境变量?,ruby-on-rails,cmd,environment-variables,secret-key,Ruby On Rails,Cmd,Environment Variables,Secret Key,我在~/.bashrc中保存了一些ENV,请转到重新打开该文件,我可以在那里看到它们: export SECRET_KEY_BASE="secretkeyhere" export S3_SECRET_KEY="anotherhere" export S3_ACCESS_KEY="andhere" export DEVISE_KEY="and again" 然而,当我告诉我的rails应用程序使用它们时,例如我的模型中带有ENV[“s3\u ACCESS\u KEY”]的s3凭据,它似乎没有使用

我在~/.bashrc中保存了一些
ENV
,请转到重新打开该文件,我可以在那里看到它们:

export SECRET_KEY_BASE="secretkeyhere"
export S3_SECRET_KEY="anotherhere"
export S3_ACCESS_KEY="andhere"
export DEVISE_KEY="and again"
然而,当我告诉我的rails应用程序使用它们时,例如我的模型中带有
ENV[“s3\u ACCESS\u KEY”]
的s3凭据,它似乎没有使用它们,给了我一个错误:

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.

当我在shell中运行
echo$designe_KEY
时,它只返回一个空行,如果我运行
source~/.bashrc
那么
echo$designe_KEY
它会返回密钥吗?!?所以它似乎就在那里的某个地方,但是rails和/或正确的会话/环境/程序无法访问它。作为UNIX、Rails以及更多的初级用户,我现在不知道这里发生了什么。请你帮我理解我做错了什么。谢谢。

作为在应用程序中处理环境变量的替代方法,我建议您看看

不必处理~/.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)

希望有帮助

您是否尝试寻找bashrc文件的来源?我尝试在bash窗口中运行
source~/.bashrc
,然后
echo$designe\u KEY
将返回密钥。但是Rails仍然返回相同的
AWS::Errors::MissingCredentialsError在PostsController中#create
error?