title: Downloading a pdf file from AWS S3 using NodeJS
body: <p>I'm trying to download a pdf file from S3, but everytime I download it to my local machine, I receive the error, &quot;Error: Failed to load PDF&quot;. It seems I am still downloading a blob instead of a file, but have tried everything and looked through too many stackoverflow solutions.</p>
  <p>Here is my backend code where the download function lives (sorry, it's still messy):
  backend --&gt; middleware --&gt; amazon.js</p>
  <pre><code>const AWS = require('aws-sdk');
  const fs = require('fs');
  const uuid = require('uuid');
  require('dotenv').config()
  console.log(&quot;proccess&quot;, process.env.IAM_USER_KEY)
  var dataUriToBuffer = require('data-uri-to-buffer');
  const _path = require('path');
  const { post } = require('../routes/user.data');
  const { app } = require('firebase-admin');
  const multer = require('multer');
  
  var FileReader = require('filereader')
      , fileReader = new FileReader
      ;
  
  AWS.config.update({
      accessKeyId: process.env.IAM_USER_KEY,
      secretAccessKey: process.env.IAM_USER_SECRET,
      region: 'us-east-1'
  })
  
  const s3 = new AWS.S3()
  
  async function upload(req, res, next){
      let key = req.params.source+&quot;/&quot;+req.params.data.fields.id;
  
      // function to save the user uploaded file to &quot;uploads&quot; folder
  
      const uploadFile = (fileName) =&gt; {
          
          // Read content from the file
          const fileContent = fs.readFileSync(fileName);        
      
          // Setting up S3 upload parameters
          const params = {
              Bucket: process.env.BUCKET,
              Key: key, // File name you want to save as in S3
              Body: fileContent
          };
      
          // Uploading files to the bucket
          s3.upload(params, function(err, data) {
              if (err) {
                  throw err;
              }
              console.log(`File uploaded successfully. ${data.Location}`);
          });
      };
      var filePath = _path.resolve(req.params.data.file.name);
      uploadFile(req.params.file_path)
  }
  
  async function download(req, res, next){
      let report_object = req.body.object_id
      report_object = report_object[0]
      let source = req.body.source
  
      let dir = _path.join(__dirname, '..', 'public')
      let fileName = `${source}.pdf`
      let filePath = _path.join(dir, fileName)
  
      let writeStream = fs.createWriteStream(filePath)
  
      var params = {
          Key: source+&quot;/&quot;+report_object,
          Bucket: process.env.BUCKET
      }
      const readStream = s3.getObject(params).createReadStream();
      
      // Error handling in read stream
      readStream.on(&quot;error&quot;, (e) =&gt; {
          console.error(e);
          reject(e);
      });
      
      readStream.pipe(writeStream);
      writeStream.on('finish', () =&gt; {
          res.download(dir + &quot;/&quot; + fileName)
      })
  }
  
  module.exports = { upload, download }
  </code></pre>
  <p>And here is my download function in the api interface that is found in the frontend and communicates with the backend:</p>
  <pre><code>import axios from 'axios';
  import fs from 'fs'
  import streamToBlob from 'stream-to-blob'
  
  
  async function upload(data, user){
      data[&quot;user&quot;] = user
      await axios({
          method: 'post', 
          url: 'http://localhost:3000/s3/upload', 
          data: data,
          headers: { &quot;content-type&quot;: &quot;multipart/form-data&quot;},
      })
  }
  
   function download(id, source){
      let data =  axios.post(&quot;http://localhost:3000/s3/download&quot;, {object_id: id, source: source}, { headers : {'Accept' : &quot;application/pdf&quot;}}).then( (res) =&gt; {
          let blob = new Blob([res.data], {type: 'application/pdf'})
          const link = document.createElement('a')
          link.href = URL.createObjectURL(blob)
          link.download = `${source}`
          link.click()
          setTimeout(function() {
              URL.revokeObjectURL(link.href)
          }, 60000)
          
          return
      })
  }
  
  export {upload, download}
  </code></pre>
  <p>I believe that the current download is a blob because I see that in the network tab of the dev inspect tools.</p>
  <p>Thank you in advance for your help.</p>