Categories
JavaScript Answers

How to download a file to disk from an AWS S3 bucket with Node?

Spread the love

To download a file to disk from an AWS S3 bucket with Node, we call the getObject method.

For instance, we write

const s3 = new AWS.S3();
const s3Params = {
  Bucket: "your bucket",
  Key: "path/to/the/file.ext",
};

s3.getObject(s3Params, (err, res) => {
  if (err === null) {
    res.attachment("file.ext");
    res.send(data.Body);
  } else {
    res.status(500).send(err);
  }
});

to call the getObject method with the Bucket and Key of the item to get.

We get the data from the data.Body property.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.