Last updated on Nov. 3, 2021, 6:24 a.m. by rugved
This blog has two parts:
Part I — Learn how to set up AWS S3 and manually use it.
Part II — Using Node.js programmatically to perform operations on S3.
In this blog we will see Node.js implementation to do the following:
mkdir nodejs-s3
cd nodejs-s3
npm init -y
npm i aws-sdk
Create index.js file in current project directory.
1. First thing we need to do is import aws-sdk package
const AWS = require('aws-sdk');
2. Now to connect with the AWS S3 we need “accessKeyId” and “secretAccessKey”. We have discussed how to get it in the previous blog.
const s3 = new AWS.S3({
accessKeyId: "ENTER YOUR accessKeyId",
secretAccessKey: "ENTER YOUR secretAccessKey",
});
3. Enter bucket name we had created in previous blog.
In my case it was “my-unique-test-bucket-123”
const BUCKET_NAME = 'my-unique-test-bucket-123';
I) Function to create a bucket programatically. It takes bucket name as input
NOTE: To set up basic bucket configuration, refer previous blog.
const createBucket = (bucketName) => {
// Create the parameters for calling createBucket
var bucketParams = {
Bucket : bucketName
};
// call S3 to create the bucket
s3.createBucket(bucketParams, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Location);
}
});
}
// createBucket("random-name-bucket-234389")
II) Lets now write a function to list all the buckets we have on S3.
const listBuckets = (s3) => {
s3.listBuckets(function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Buckets);
}
});
}
// listBuckets(s3)
This function will list all the buckets in your AWS S3 dashboard.
III) Next lets write function to upload image. For this pick any image and place it in project directory. (same directory as index.js)
const uploadFile = (filePath,bucketName,keyName) => {
var fs = require('fs');
// Read the file
const file = fs.readFileSync(filePath);
// Setting up S3 upload parameters
const uploadParams = {
Bucket: bucketName, // Bucket into which you want to upload file
Key: keyName, // Name by which you want to save it
Body: file // Local file
};
s3.upload(uploadParams, function(err, data) {
if (err) {
console.log("Error", err);
}
if (data) {
console.log("Upload Success", data.Location);
}
});
};
// uploadFile('florian-olivo-4hbJ-eymZ1o-unsplash.jpg',BUCKET_NAME,"code.jpg")
The above uploadFile function takes two parameters:
- filePath: Local file’s path to be uploaded to the bucket.
- bucketName: Bucket into which file needs to be uploaded.
IV) To list all the objetcs in a particular bucket we will create a function which will take single parameter “bucketName” as input.
const listObjectsInBucket = (bucketName) => {
// Create the parameters for calling listObjects
var bucketParams = {
Bucket : bucketName,
};
// Call S3 to obtain a list of the objects in the bucket
s3.listObjects(bucketParams, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
}
// listObjectsInBucket(BUCKET_NAME)
V) Delete a bucket. Takes bucket name as input.
const deleteBucket = (bucketName) => {
// Create params for S3.deleteBucket
var bucketParams = {
Bucket : bucketName
};
// Call S3 to delete the bucket
s3.deleteBucket(bucketParams, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
}
// deleteBucket("random-name-bucket-234389")
Now that we have all the functions ready we can use it individually. Just for easy demonstration i have built a function which will run all the above discussed functions one by one at an interval of 5 seconds.
const AWS = require('aws-sdk');
const s3 = new AWS.S3({
accessKeyId: "ENTER YOUR accessKeyId",
secretAccessKey: "ENTER YOUR secretAccessKey",
});
const BUCKET_NAME = 'random-name-bucket-234389'
const createBucket = (bucketName) => {
// Create the parameters for calling createBucket
var bucketParams = {
Bucket : bucketName
};
// call S3 to create the bucket
s3.createBucket(bucketParams, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Location);
}
});
}
const listBuckets = (s3) => {
s3.listBuckets(function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.Buckets);
}
});
}
const uploadFile = (filePath,bucketName,keyName) => {
var fs = require('fs');
// Read the file
const file = fs.readFileSync(filePath);
// Setting up S3 upload parameters
const uploadParams = {
Bucket: bucketName, // Bucket into which you want to upload file
Key: keyName, // Name by which you want to save it
Body: file // Local file
};
s3.upload(uploadParams, function(err, data) {
if (err) {
console.log("Error", err);
}
if (data) {
console.log("Upload Success", data.Location);
}
});
};
const listObjectsInBucket = (bucketName) => {
// Create the parameters for calling listObjects
var bucketParams = {
Bucket : bucketName,
};
// Call S3 to obtain a list of the objects in the bucket
s3.listObjects(bucketParams, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
}
const deleteBucket = (bucketName) => {
// Create params for S3.deleteBucket
var bucketParams = {
Bucket : bucketName
};
// Call S3 to delete the bucket
s3.deleteBucket(bucketParams, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
}
function sleep(ms) {
console.log('Wait...')
return new Promise(resolve => setTimeout(resolve, ms));
}
async function main(){
console.log('\nCreating bucket : ')
createBucket(BUCKET_NAME)
await sleep(5000)
console.log('\nListing out all the buckets in your AWS S3: ')
listBuckets(s3)
await sleep(5000)
console.log('\nUploading image1 to '+ BUCKET_NAME)
uploadFile('daniel-norin-lBhhnhndpE0-unsplash.jpg',BUCKET_NAME,"football.jpg")
await sleep(5000)
console.log('\nUploading image2 to '+ BUCKET_NAME)
uploadFile('florian-olivo-4hbJ-eymZ1o-unsplash.jpg',BUCKET_NAME,"code.jpg")
await sleep(5000)
console.log('\nListing out all the files/objects in the bucket '+ BUCKET_NAME)
listObjectsInBucket(BUCKET_NAME)
await sleep(5000)
}
main()
DONE! That’s all, my friend, for this blog.
If you found this blog helpful, feel free to clap, and share it with your friends.
So, we have successfully learned how to use Node.js to create an S3 bucket, upload files, list buckets, list objects, and delete bucket.
For the code, you can visit: https://github.com/RugvedB/Nodejs-s3