
NodeJS uses the aws-sdk package to interact with Amazon Web Services (AWS). The npm list command to determine if the aws-sdk package is installed on your system.
~]$ npm list
/tmp/foo
└── (empty)
If the aws-sdk package is not listed, the npm install command can be used to install the aws-sdk package. For example, let's say you want to do something with S3 Buckets. In this scenario, you would install the S3 client.
npm install @aws-sdk/client-s3
And now the npm list command should show that the aws-sdk S3 client has been installed in NodeJS.
]$ npm list
foo@ /tmp/foo
└── @aws-sdk/client-s3@3.600.0
Here is the minimal boilerplate code without any error handling to list your S3 Buckets using NodeJS using CommonJS.
const { S3Client, ListBucketsCommand } = require("@aws-sdk/client-s3");
const client = new S3Client({ region: "us-east-1" });
const params = {};
const command = new ListBucketsCommand(params);
async function myBuckets() {
const data = await client.send(command);
console.log(data);
}
myBuckets()
If your app is an ES module, package.json will have "type": "module".
{
"type":"module",
"dependencies": {
"@aws-sdk/client-s3": "^3.600.0"
}
}
In this scenario, you will use import instead of require.
import { S3Client, ListBucketsCommand } from "@aws-sdk/client-s3";
Which should return something like this.
o]$ node app.js
{
'$metadata': {
httpStatusCode: 200,
requestId: 'J4NWGT3E8PJNN2QK',
extendedRequestId: '2ESieLh...aj6u0w0=',
cfId: undefined,
attempts: 1,
totalRetryDelay: 0
},
Buckets: [
{
Name: 'my-bucket-abc',
CreationDate: 2024-04-19T01:38:37.000Z
},
{
Name: 'my-bucket-123',
CreationDate: 2023-07-21T23:17:02.000Z
}
],
Owner: {
DisplayName: 'john.doe',
ID: 'ab0e0a4...43743'
}
}
So, how does authentication work? Let's say you have set Amazon Web Services (AWS) Profile Config on the same system as your NodeJS program.
- Linux / Mac = /home/john.doe/.aws/config
- Windows = C:\Users\your_username\.aws\config (if this file does not exist, it can be created using the PowerShell New-Item and Add-Content cmdlets)
For example, let's say the "config" file contains something like this.
[default]
region = us-east-1
profile = johndoe
output = json
[profile johndoe]
region = us-east-1
output = json
[profile janedoe]
region = us-east-1
output = json
Likewise, the "credentials" file contains something like this.
- Linux / Mac = /home/john.doe/.aws/credentials
- Windows = C:\Users\your_username\.aws\credentials
[default]
aws_secret_access_key = abcdefg123456789abcdefg123456789abcdefg1
aws_access_key_id = ABCDEFG123456789ABCD
[johndoe]
aws_secret_access_key = zxcasdqwe987654321zxsdqwe3213654987zxcsd
aws_access_key_id = DFMVKFJSM456789SDFSB
[janedoe]
aws_secret_access_key = pftasdqwe987654321zxsdqwe3213654987zxzya
aws_access_key_id = AKB123ABLC2349dAD93Z
In this scenario, if you do not include "profile" then the "default" profile will be used.
const client = new S3Client({ region: "us-east-1" });
Or, you can include a named profile.
const client = new S3Client({ region: "us-east-1", profile: "janedoe" });
Did you find this article helpful?
If so, consider buying me a coffee over at