简体   繁体   中英

How can i get my Rails app to use my ~/.bashrc saved ENV VARS?

I have saved some ENV 's in ~/.bashrc, go to reopen the file and i can see them there:

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

Yet when i tell my rails app to use them, as for example s3 credentials in my model with ENV["S3_ACCESS_KEY"] it seemingly doesn't use them, giving me the error:

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.

When in shell i run echo $DEVISE_KEY and it returns nothing but an empty line, if i run source ~/.bashrc then echo $DEVISE_KEY it does return the key?!? So it seems it is in there somewhere, but not accessible by rails and or the right session/environment/program. As a junior to UNIX, Rails and much more I'm now at a loss as to what's happening here. Please can you help me understand what i'm doing wrong here. Thankyou.

As an alternative approach to handle environment variables within your application, I suggest you have a look at dotenv gem .

Instead of having to deal with ~/.bashrc files (which in your case is certainly not being loaded correctly), you can have a .env file in your local environment where you define the variables and that will be loaded into ENV when the environment is bootstrapped. You don't commit your .env file to your version control and keep it local to your environment.

In your case you'll add your variables to .env

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

and then just call them in your AWS config file like this:

config.secret_key  = ENV['S3_SECRET_KEY'] 

(or whatever the config method is)

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM