Node.js

aws | node.js | S3 file download

Hello,

Hope all are fine. This article is for writing a node script to download a file from s3 bucket.

Package needed

npm install aws-sdk

Package usage

const AWS = require('aws-sdk')

Code Example

        const filename = "test.pdf"
const params = {
Bucket: "mybucket",
Key: `${filename}`,
}

// Retrieve the PDF file from S3
s3.getObject(params, (err, data) => {
if (err) {
console.error(err)
return res.status(500).send('Error retrieving file from S3')
}

// // Set the appropriate headers for the response
res.setHeader('Content-disposition', `attachment; filename=${filename}`)
res.setHeader('Content-type', 'application/pdf')

// Send the file content as the response
res.send(data.Body)
})

Hope its very simple and useful.

The same article in on my medium at

https://medium.com/@vipinc.007/aws-node-js-s3-file-download-2260391852b9

Leave a Reply

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