Categories
JavaScript Answers

How to download a file from Amazon S3 bucket with JavaScript?

Spread the love

Sometimes, we want to download a file from Amazon S3 bucket with JavaScript.

In this article, we’ll look at how to download a file from Amazon S3 bucket with JavaScript.

How to download a file from Amazon S3 bucket with JavaScript?

To download a file from Amazon S3 bucket with JavaScript, we use the AWS Node.js API.

For instance, we write

const AWS = require("aws-sdk");

AWS.config.update({
  accessKeyId: "your key",
  secretAccessKey: "your secret key",
});

const s3 = new AWS.S3();

s3.getObject({ Bucket: "my-bucket", Key: "my-picture.jpg" }, (error, data) => {
  if (error !== null) {
    console.log("Failed to retrieve an object: ", error);
  } else {
    console.log("Loaded " + data.ContentLength + " bytes");
  }
});

to call AWS.config.update to set the keys for accessing S3.

to call getObject with with the bucket and key name to download the file with the given key in the given buckey.

We get the error if there is any.

Conclusion

To download a file from Amazon S3 bucket with JavaScript, we use the AWS Node.js API.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published. Required fields are marked *