NodeJS Amazon AWS S3 getObject how to send file in API response to download

0 votes

I am building a NodeJS Express API that accepts a filepath as a parameter, gets an object from S3 and then sends the file back in the API response.

I am using the NodeJS Amazon AWS S3 SDK @aws-sdk/client-s3.

I have everything working up to returning the file in the API response.

Here is the controller. This is working to get the object as a readable stream from S3.

Question:

How do I return the file in the Node JS Express API response to download?

Note: I've already tried other suggestions in a few other questions but none of the solutions have worked to return the file in the API response.

I tried the answer in this question here but the file is downloaded on the server side, the file needs to be returned in the API response. How to save file from S3 using aws-sdk v3

Also the answer in this question here is using the v2 version of the SDK and this is v3. There is no .createReadStream() method anymore. NodeJS download file from AWS S3 Bucket

Mar 21, 2022 in Others by Edureka
• 13,690 points
9,748 views

1 answer to this question.

+1 vote

Server Side

const aws = require('aws-sdk');

router.get('/getfilefroms3', async (req, res, next) => {

  aws.config.update({
    secretAccessKey: config.secret_access_key,
    accessKeyId: config.access_key_id,
    signatureVersion: config.signature_version,
    region: config.region
  })

  const s3 = new aws.S3({ });

  var params = { Bucket: config.sample_bucket_name, Key: req.query.filename };

  s3.getObject(params, function (err, data) {
    if (err) {
      res.status(200);
      res.end('Error Fetching File');
    }
    else {
      res.attachment(params.Key); // Set Filename
      res.type(data.ContentType); // Set FileType
      res.send(data.Body);        // Send File Buffer
    }
  });

})

Client Side

If you are using Web Application you can use any HTTP REST API Client like Axios or Fetch, The Download Manager will capture the file.

curl --location --request GET 'http://localhost:5001/getfilefroms3?filename=sample.pdf'

If you are using NodeJS Application

var http = require('http');
var fs = require('fs');

var download = function (url, destination, callback) {
    var file = fs.createWriteStream(destination);
    http.get(url, function (response) {
        response.pipe(file);
        file.on('finish', function () {
            file.close(callback);
        });
    });
}

var fileToDownload = "sample.pdf"

download("http://localhost:5001/getfilefroms3?filename=" + fileToDownload, "./" + file
answered Mar 24, 2022 by gaurav
• 23,260 points

Related Questions In Others

0 votes
0 answers

How to get response from S3 getObject in Node.js?

I am attempting to get data back ...READ MORE

Apr 23, 2022 in Others by Kichu
• 19,040 points
1,863 views
0 votes
0 answers

How can I re-download the pem file in AWS EC2?

I created a key pair pem file ...READ MORE

Apr 5, 2022 in Others by Kichu
• 19,040 points
2,236 views
0 votes
1 answer
0 votes
1 answer

How to unistall a file in Linux?

You can uninstall a file using the ...READ MORE

answered Mar 8, 2019 in Others by Nabarupa
1,418 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,916 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,445 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,417 views
0 votes
1 answer

How to use AWS S3 CLI to dump files to stdout in BASH?

All file objects must have their contents ...READ MORE

answered Mar 24, 2022 in Others by gaurav
• 23,260 points
2,151 views
0 votes
1 answer

How to import excel file in Oracle SQL live

Hello, there are a few steps You'll ...READ MORE

answered Feb 18, 2022 in Others by gaurav
• 23,260 points
2,136 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP