title: Error while downloading multiple files from AWS S3 using Nodejs
body: <p>I am getting error "File download failed with error write after end" while trying to download multiple files form AWS s3 using the below code snippet.</p>
  
  <p>Can some one help me to figure out the root cause of the error?It would be great if any one can suggest a solution as well</p>
  
  <p>Below code works without any issues if only a single file needs to be downloaded</p>
  
  <pre><code>var AWS = require('aws-sdk');
  var fs = require('fs')
  
  module.exports.download = function (req, res) {
  
  var S3_BUCKET = 'mybucketname'
  var s3 = new AWS.S3({
      accessKeyId: process.env.ACCESSKEY,
      secretAccessKey: process.env.SECRETKEY,
      region: process.env.REGION
  });
  
  var os = require('os');
  var filenames = "file1.jpg,file2.jpg"
  
  var str_array = filenames.split(','); 
  for (var i = 0; i &lt; str_array.length; i++) {
      var filename = str_array[i].trim();
  
      localFileName = os.homedir() + "\\" + "Downloads" + "\\" + filename,
  
      file = fs.createWriteStream(localFileName);
      s3.getObject({
          Bucket: S3_BUCKET,
          Key: filename
      })
      .on('error', function (err) {
          res.end("File download failed with error " + err.message);            
      })
      .on('httpData', function (chunk) {
          file.write(chunk);
      })
      .on('httpDone', function () {
          file.end();
      })
      .send();
  }
    res.end("Files have been downloaded successfully")
   }
  </code></pre>