Categories
JavaScript Answers

How to read file from aws s3 bucket using Node fs?

Spread the love

To read file from aws s3 bucket using Node fs, we create a write stream.

For instance, we write

const s3 = new AWS.S3({ apiVersion: "2006-03-01" });
const params = { Bucket: "myBucket", Key: "myImageFile.jpg" };
const file = require("fs").createWriteStream("/path/to/file.jpg");
s3.getObject(params).createReadStream().pipe(file);

to call createWriteStreamn with the path to write the data to.

Then we call getObject to get the object by the bucket and key.

And we call createReadStream to read the file and call pipe to pipe the file to the file write stream to write it.

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 *