How to use the Credentials function from aws-sdk

Find comprehensive JavaScript aws-sdk.Credentials code examples handpicked from public code repositorys.

aws-sdk.Credentials is a constructor function in the AWS SDK for JavaScript that creates an object representing AWS credentials used to access AWS services.

583
584
585
586
587
588
589
590
591
592
const awsProvider = this.getAwsProvider();
const tmpCreds = awsProvider.getCredentials();
if (!tmpCreds.credentials){
  const accessKeyId = process.env.AWS_ACCESS_KEY_ID || 'test';
  const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY || 'test';
  const fakeCredentials = new AWS.Credentials({accessKeyId, secretAccessKey});
  configChanges.credentials = fakeCredentials;
  // set environment variables, ...
  process.env.AWS_ACCESS_KEY_ID = accessKeyId;
  process.env.AWS_SECRET_ACCESS_KEY = secretAccessKey;
fork icon81
star icon457
watch icon0

+ 37 other calls in file

48
49
50
51
52
53
54
55
56
57
}).promise());
if (_.isEmpty(creds.Credentials)) {
    dbg.error(`AWS STS empty creds ${params.RoleArn}, RolesessionName: ${params.RoleSessionName},Projected service Account Token Path : ${projectedServiceAccountToken}`);
    throw new RpcError('AWS_STS_ERROR', 'Empty AWS STS creds retrieved for Role "' + params.RoleArn + '"');
}
return new AWS.Credentials(
    creds.Credentials.AccessKeyId,
    creds.Credentials.SecretAccessKey,
    creds.Credentials.SessionToken
);
fork icon68
star icon227
watch icon17

+ 37 other calls in file

How does aws-sdk.Credentials work?

aws-sdk.Credentials is a constructor function in the AWS SDK for JavaScript that creates an object representing AWS credentials used to access AWS services. When you create an instance of aws-sdk.Credentials, you provide the AWS access key ID and secret access key, which are used to authenticate your access to AWS services. You can also provide a session token, which is used for temporary credentials obtained via IAM roles or other mechanisms. The aws-sdk.Credentials object provides methods for refreshing the credentials, which is typically done automatically by the SDK when the credentials are about to expire. The object also provides methods for retrieving the access key ID, secret access key, and session token, as well as for checking whether the credentials are expired or not. The aws-sdk.Credentials object can be passed to other AWS SDK objects that require credentials, such as aws-sdk.S3 or aws-sdk.DynamoDB. These objects will use the credentials provided by the aws-sdk.Credentials object to authenticate your access to AWS services. In essence, aws-sdk.Credentials provides a way to manage AWS credentials used to access AWS services in JavaScript applications. It enables you to securely store and manage your AWS access keys and secret access keys, and automatically refreshes the credentials when they are about to expire.

16
17
18
19
20
21
22
23
24
25
constructor(accessKey, secretKey) {
  this.accessKey = accessKey;
  this.secretKey = secretKey;

  AWS.config.update({
    credentials: new AWS.Credentials({
      accessKeyId: accessKey,
      secretAccessKey: secretKey,
    }),
  });
fork icon14
star icon134
watch icon5

26
27
28
29
30
31
32
33
34
35
    ms = new AWS.MetadataService();
    ms.request('/latest/meta-data/iam/security-credentials/' + this.config.iamRole, function(err, rdata) {
      var data = JSON.parse(rdata);

      if (err) { console.log('Failed to fetch IAM role credentials: ' + err); }
      self.config.credentials = new AWS.Credentials(data.AccessKeyId, data.SecretAccessKey, data.Token);
      setEmitter();
    });
  }
} else {
fork icon53
star icon80
watch icon5

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const AWS = require("aws-sdk");

const accessKeyId = "YOUR_ACCESS_KEY_ID";
const secretAccessKey = "YOUR_SECRET_ACCESS_KEY";
const sessionToken = "YOUR_SESSION_TOKEN";

const credentials = new AWS.Credentials({
  accessKeyId: accessKeyId,
  secretAccessKey: secretAccessKey,
  sessionToken: sessionToken,
});

console.log(credentials.accessKeyId);
// Output: YOUR_ACCESS_KEY_ID

console.log(credentials.secretAccessKey);
// Output: YOUR_SECRET_ACCESS_KEY

console.log(credentials.sessionToken);
// Output: YOUR_SESSION_TOKEN

In this example, we first import the aws-sdk library. We then define accessKeyId, secretAccessKey, and sessionToken strings that represent AWS credentials used to access AWS services. We then create a new instance of aws-sdk.Credentials by passing an object with the accessKeyId, secretAccessKey, and sessionToken properties. We then call credentials.accessKeyId, credentials.secretAccessKey, and credentials.sessionToken to retrieve the respective credential values. The function returns the values of the properties we defined earlier. Note that in this example, we're using hard-coded values for the AWS credentials, but in a real application you would typically store these values securely and retrieve them as needed.

77
78
79
80
81
82
83
84
85
    return awsS3Index(context)
        .then(() => {
            assert.strictEqual(optionsParam.region, 'us-west-1');
            assert.deepStrictEqual(
                optionsParam.credentials,
                new AWS.Credentials({ accessKeyId: 'awsuser', secretAccessKey: 'awssecret' })
            );
        });
});
fork icon23
star icon49
watch icon20

+ 19 other calls in file

24
25
26
27
28
29
30
31
32
33
var self = {
  init: function (options, callback) {
    // knox bc
    endpoint = 's3.amazonaws.com';
    if (options.secret) {
      options.credentials = new AWS.Credentials(options.key, options.secret, options.token || null);
    }
    bucket = options.bucket;
    bucketObjectsACL = options.bucketObjectsACL || 'public-read';
    options.params = options.params || {};
fork icon31
star icon47
watch icon13

35
36
37
38
39
40
41
42
43
44
45
const sixtyDaysInSeconds = 5184000
app.use(helmet.hsts({
    maxAge: sixtyDaysInSeconds
}))


AWS.config.credentials = new AWS.Credentials(process.env.AWSAccessKeyId, process.env.AWSSecretKey);
// the AWS Chime SDK documentation states the instance of the AWS.Chime object
// currently must be configured to us-east-1, however the actual region where the
// meetings are hosted can be configured when invoking Chime.createMeeting
const chime = new AWS.Chime({region: 'us-east-1'});
fork icon0
star icon0
watch icon1

+ 4 other calls in file

17
18
19
20
21
22
23
24
25
26
  username: String,
  password: String,
  email: String,
  device_id: String,
});
const access = new aws.Credentials({
  accessKeyId: "",
  secretAccessKey: "",
});
const s3 = new aws.S3({
fork icon0
star icon0
watch icon0

22
23
24
25
26
27
28
29
30
31
32
33
	}
};


// Configure aws-sdk to fail-fast
const AWS = require('aws-sdk');
AWS.config.credentials = new AWS.Credentials({ accessKeyId: "foo", secretAccessKey: "bar" });
// Mock console.error to hide expected errors
console.error = jest.fn();


require("./_parser_mock")
fork icon0
star icon0
watch icon0

17
18
19
20
21
22
23
24
25
26

function getS3Client () {
  s3Client ??= new aws.S3({
    signatureVersion: 'v4',
    region: process.env.COMPANION_AWS_REGION,
    credentials : new aws.Credentials(
      process.env.COMPANION_AWS_KEY,
      process.env.COMPANION_AWS_SECRET,
    ),
  })
fork icon0
star icon0
watch icon332

79
80
81
82
83
84
85
86
87
88
let credentials = null;
if (useAWSInstanceProfile) {
  credentials = new AWS.EC2MetadataCredentials({ httpOptions: { timeout: 5000 } });
} else if (useRoleArn) {
  const assumeRoleCredentials = await this.getAssumeRoleCredentials(sourceOptions['role_arn']);
  credentials = new AWS.Credentials(
    assumeRoleCredentials.accessKeyId,
    assumeRoleCredentials.secretAccessKey,
    assumeRoleCredentials.sessionToken
  );
fork icon0
star icon0
watch icon147

+ 3 other calls in file