Categories
JavaScript Answers

How to create folder or key on s3 using AWS SDK for Node.js?

Spread the love

To create folder or key on s3 using AWS SDK for Node.js, we call the upload method.

For instance, we write

const AWS = require("aws-sdk");
AWS.config.region = "us-east-1";
const s3Client = new AWS.S3();

const params = {
  Bucket: "your_bucket_goes_here",
  Key: "folderInBucket/",
  ACL: "public-read",
  Body: "body does not matter",
};

s3Client.upload(params, (err, data) => {
  if (err) {
    console.log("Error creating the folder: ", err);
  } else {
    console.log("Successfully created a folder on S3");
  }
});

to call upload with the params object that has the Bucket and Key of the file.

And Body has the body of the file we’re uploading.

We get the uploaded data from data and errors from err in the callback.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.