问题
change log says
Load config from ~/.aws/config if AWS_SDK_LOAD_CONFIG is set.
Couldn't find any examples or documentation regarding how to load the config. Any help!
回答1:
There is a little bit of magic in how aws-sdk loads the config
either set the env variable
export AWS_SDK_LOAD_CONFIG="true"
or before loading the aws-sdk set
process.env.AWS_SDK_LOAD_CONFIG = true;
Then load the aws module;
var AWS = require('aws-sdk');
You can access the region directly by
AWS.config.region
回答2:
The answer of sreenivas is correct. It also seems to be the only way to do this without writing a custom function.
I've traced it back in the source code and the the way it loads ~/.aws/config
is similar to this pseudocode:
if process.env.AWS_SDK_LOAD_CONFIG:
return load('~/.aws/credentials').overwrite('~/.aws/config')
else:
return load('~/.aws/credentials')
This also means you can set the environment variable after require('aws-sdk')
, as long as you do it before new SharedIniFileCredentials({..})
or credentials.refresh()
. Beware that credentials.get()
will not work until the sessiontoken has expired.
回答3:
There is documentation for this:
- HERE: Loading Node Credentials Shared
- HERE: Loading Node Credentials JSON File
- HERE: Getting Started NodeJS
I would recommend installing the awscli tool to set this up and then run aws configure
in your terminal. By default, anything you're running on your local host will assume the credentials listed in your config
file unless specified to assume a different profile.
Example from the first link:
AWS.config.credentials = new AWS.SharedIniFileCredentials( { profile: 'work-account' } );
If you are using the CLI to run your script:
AWS_PROFILE=work-account node script.js
If you are using just the CLI tool and not JavaScript:
aws s3 ls --profile work-account
Update:
config
and credentials
are made and referenced at the same time. When you run aws configure
it makes two files. The credentials
file containing AccessKey and SecretKey - and the config
file containing the response type and region. It is NOT necessary to explicitly define or reference the config
file.
来源:https://stackoverflow.com/questions/44433527/how-to-load-config-from-aws-config