Sometimes, we want to download a file to disk from an AWS S3 bucket with Node.js.
In this article, we’ll look at how to download a file to disk from an AWS S3 bucket with Node.js.
How to download a file to disk from an AWS S3 bucket with Node.js?
To download a file to disk from an AWS S3 bucket with Node.js, we can use 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 getObject
with an object with the Bucket
and Key
to the file.
Then we get the data from res
and call res.attachment
to write the file data to the path.
And then we call res.send
to put the file data into the file.
Conclusion
To download a file to disk from an AWS S3 bucket with Node.js, we can use the getObject
method.