title: Uploading Multiple Files To S3 Node
body: <p>I am trying to upload multiple files to <code>S3</code> from my <code>Sails.js</code>application. I've a Controller to handle the Request and a <code>Service</code> to upload should in case multiple controllers wants to utilize the function. The promise in the controller doesn't get resolved. And also, I keep getting this error <strong>Unhandled rejection TimeoutError: Missing credentials in config</strong></p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>multiSizeCreate: (req, res) =&gt; {
  
      UploadService.uploadFile(req).then((data, failure) =&gt; {
          if (failure) return [null];
          return [Promise.resolve(AWSService.s3awsupload(data))];&lt;-- &lt;--The promise doesn't get resolved
      })
      .spread(uploadedData =&gt; {
          if (!uploadedData) return ResponseService.json(403, res, 'File upload failed');
          console.log(uploadedData); &lt;--It logs an unresolved promise
          return ResponseService.json(201, res, 'Size(s) created successfully');
      })
      .catch(err =&gt; ValidationService.jsonResolveError(err, Size, res))
  }
  </code></pre>
  
  <p><strong>AWSService</strong></p>
  
  <pre><code>const AWS = require('aws-sdk');
  const s3 = new AWS.S3();
  const fs = require('fs');
  const key = sails.config.settings.aws.key;
  const secret = sails.config.settings.aws.secret;
  const bucket = sails.config.settings.aws.bucket;
  _ = require('lodash');
  AWS.config.update({accessKeyId: key, secretAccessKey: secret});
  const Promise = require('bluebird');
  
  module.exports = {
  
      s3awsupload: (files) =&gt; {
  
          let toUpload = []
          if (files.length &gt; 0) {
              _.forEach(files, file =&gt; {
                  toUpload.push(awsmainUpload(file));
              });
              return [toUpload];
          } else {
              return null;
          }
      }
  }
  
  const awsmainUpload = (fileDirectory) =&gt; {
  
      const uploadImage = fs.createReadStream(fileDirectory.fd);
      const fileName = Math.floor(Date.now() / 1000);
      const params = {Bucket: bucket, Key: fileName + " " + fileDirectory.fileName, Body: uploadImage};
  
      return new Promise((resolve, reject) =&gt; {
          s3.upload(params, (error, outpout) =&gt; {
              return error ? reject(error) : resolve(outpout);
          })
      })
  }
  </code></pre>