title: NodeJS Amazon AWS S3 getObject how to send file in API response to download
body: <p>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.</p>
  <p>I am using the NodeJS Amazon AWS S3 SDK  <code>@aws-sdk/client-s3</code>.</p>
  <p>I have everything working up to returning the file in the API response.</p>
  <p>Here is the controller. This is working to get the object as a readable stream from S3.</p>
  <pre><code>const consoleLogger = require('../logger/logger.js').console;
  const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3');
  const s3client = new S3Client({ region: process.env.AWS_REGION || 'us-east-1' });
  
  const awsCtrl = {};
  
  awsCtrl.getObject = async (key) =&gt; {
  
    // Get object from Amazon S3 bucket
    let data;
    try {
      // Data is returned as a ReadableStream
      data = await s3client.send(new GetObjectCommand({ Bucket: process.env.AWS_BUCKET, Key: key }));
      console.log(&quot;Success&quot;, data);
    } catch (e) {
      consoleLogger.error(&quot;AWS S3 error: &quot;, e);
      const awsS3Error = {
        name: e.name || null,
        status: e.$metadata.httpStatusCode || 500
      };
      throw awsS3Error;
    }
  
    return data;
  
  }
  
  module.exports = awsCtrl;
  </code></pre>
  <p>Here is where the controller is called.</p>
  <pre><code>router.get('/:fileId', (req, res, next) =&gt; {
  
    return filesCtrl.deliverFile(req, res);
  
  });
  
  filesCtrl.deliverFile = async (req, res) =&gt; {
    /* Get object from AWS S3 */
    let fileStream;
    try {
      fileStream = await awsCtrl.getObject(filePath);
    } catch (e) {
      consoleLogger.error(`Unable to get object from AWS S3`, e);
      if (e.status &amp;&amp; e.status === 404) {
        result.error = `Not found`;
        result.status = 404;
        return res.status(result.status).json(result);
      }
      return res.status(e.status || 500).json(result);
    }
  
    // Set file header
    res.attachment(filename);
  
    // HOW TO RETURN THE FILE IN API RESPONSE TO DOWNLOAD?
    // not working
    //return fileStream.pipe(res); 
  } 
  </code></pre>
  <p><strong>Question:</strong></p>
  <p>How do I return the file in the Node JS Express API response to download?</p>
  <p>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.</p>
  <p>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. <a href="https://stackoverflow.com/questions/66689614/how-to-save-file-from-s3-using-aws-sdk-v3">How to save file from S3 using aws-sdk v3</a></p>
  <p>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. <a href="https://stackoverflow.com/questions/55705550/nodejs-download-file-from-aws-s3-bucket?rq=1">NodeJS download file from AWS S3 Bucket</a></p>