Thought leadership from the most innovative tech companies, all in one place.

Using Node.js + S3 to Create, Delete, List Buckets and Upload, List Objects- Part 2

Introduction

This is the second part of the blog. To follow along, visit my previous blog to set up AWS S3 — bucket policy, configuration, etc.

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.

What are we building?

In this blog we will see **Node.js implementation **to do the following:

  1. Create bucket on S3 (like a specific unique folder to store our media)

  2. List out all the buckets made by us.

  3. Upload images to the bucket.

  4. List out all the objects(images,videos etc) in the bucket

  5. Delete the bucket.

Set up

mkdir nodejs-s3
cd nodejs-s3
npm init -y

Installing required npm packages

npm i aws-sdk

Create index.js file in current project directory.

Implementation

  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",
});
  1. 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.

Complete index.js file

Output

image

image

DONE! That's all, my friend, for this blog.

Conclusion

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




Continue Learning