download file from s3 bucket nodejs code example
Example 1: node download s3 file
const AWS = require('aws-sdk');
AWS.config.update(
{
accessKeyId: "....",
secretAccessKey: "...",
region: 'ap-southeast-1'
}
);
const s3 = new AWS.S3();
const options = {
Bucket : '/bucket-url',
Key : fileKey,
};
const fileStream = s3.getObject(options).createReadStream();
fileStream.pipe(res);
Example 2: node js read file from s3 bucket
# This function for read/download file from s3 bucketconst s3download = function (params) { return new Promise((resolve, reject) => { s3.createBucket({ Bucket: BUCKET_NAME }, function () { s3.getObject(params, function (err, data) { if (err) { reject(err); } else { console.log("Successfully dowloaded data from bucket"); resolve(data); } }); }); });}