Search code examples
amazon-web-servicesamazon-s3aws-sdkaws-cliaws-sdk-nodejs

AWS-SDK for NodeJS: how to get credentials being used in program


I am accessing AWS sdk and its services like this in my code:

var aws = require('aws-sdk');
const s3 = new aws.S3();

I want to see what are the credentials being picked up when I initialise the S3 object. I tried following ways and clearly I am unable to figure out from the documentation how to use the methods and classes properly.

var credo = aws.config.Credentials().get();
var credo = aws.config.Credentials;
var credo = aws.config.credentials;
var credo = aws.Credentials().get();
var credo = aws.Credentials();
var credo = aws.Credentials;

Can someone tell me the right way to get this data? I am not finding aws documentation easy to understand for this part.

Edit: I am able to update credentials in code using aws.config.update({accessKeyId: 'xxx', secretAccessKey: 'yyy', sessionToken:'zzz'

I want to see what these values are when I dont set them like this. Process environment variables are not set. I have credentials file set up correctly.


Solution

  • You can get the globally configured credentials from aws.config.credentials

    Get the accessKeyId:

    var accessKeyId = aws.config.credentials.accessKeyId;
    

    Get the secretAccessKey:

    var secretAccessKey = aws.config.credentials.secretAccessKey;