简体   繁体   中英

Setting AWS SDK credentials in Node

I am having trouble setting the aws credentials for a react app that needs the aws sdk. I have set up my credentials file in the '~/.aws/credentials' path, and I know this is okay. However, I don't know how to go about this in my jsx file. My understanding was that the SDK checks this credentials file on its own.

Here is my code:

process.env.AWS_SDK_LOAD_CONFIG = true; 
var AWS = require("aws-sdk");
console.log(AWS.config)

But when I log the AWS.config object, I see credentials: null, region: null

Image of console log

I'd really appreciate any help!

Are you sure everything is fine with your.aws/config and.aws/credentials files? I've run your code and I got below result. Please note I don't have the config file so region is undefined.

Config {
  credentials: 
   SharedIniFileCredentials {
     expired: false,
     expireTime: null,
     accessKeyId: 'xxx',
     sessionToken: undefined,
     filename: '/home/juzeff/.aws/credentials',
     profile: 'default',
     disableAssumeRole: true },
  credentialProvider: 
   CredentialProviderChain {
     providers: [ [Function], [Function], [Function], [Function] ] },
  region: undefined,

If you load the credentials with AWS_SDK_LOAD_CONFIG make sure you have the [default] profile specified in your credentials file. If you have multiple profiles load the one you want to use this way:

const profile = 'corporate-bucket';
const credentials = new AWS.SharedIniFileCredentials({ profile });
AWS.config.credentials = credentials;

assuming your.aws/credentials file looks like this:

[corporate-bucket]
aws_access_key_id = xxx
aws_secret_access_key = yyy

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