title: How to synchronously upload files to S3 using aws-sdk?
body: <p>I'm attempting to upload files to my S3 bucket and then return out of my upload function. The problem is that I'm returning out of the function before the upload returns the stored data. </p>
  
  <p>I've attempted to use <code>async/await</code> with <code>s3.upload</code>, but I don't believe <code>s3.upload</code> is a promise so it doesn't do anything.</p>
  
  <p>ex: </p>
  
  <pre><code>   for (const file of files) {
      const params = {
        Bucket: BUCKET_NAME,
        Key: file.name,
        Body: file.data
      };
      const stored = await s3.upload(params, (err, data) =&gt; {
        if (err) console.log("error", err, err.stack);
        return console.log(data);
      });
      console.log(stored);
     }
     console.log("leave loop")
  </code></pre>
  
  <p>I've attempted using the Async utility module with <code>async.each</code> and <code>async.eachSeries</code> as referenced <a href="https://stackoverflow.com/questions/34839980/how-to-upload-files-to-s3-synchronously-using-node-js-api">in this stack answer</a> like so:</p>
  
  <pre><code>  const saveFile = async file =&gt; {
      const params = {
        Bucket: BUCKET_NAME,
        Key: file.name,
        Body: file.data
      };
      s3.upload(params, (err, data) =&gt; {
        if (err) console.log("error", err, err.stack);
        console.log(data);
      });
    };
  
    await async.eachSeries(files, saveFile, err =&gt; {
      if (err) console.log(err);
    });
  
    console.log("leave loop")
  </code></pre>
  
  <p>But it is still hitting "leave" too soon. I'm misunderstanding something but I'm not sure what.</p>
  
  <p><strong>Update:</strong></p>
  
  <p><code>s3.upload</code> returns a <code>managedUpload</code> object - that seems to be why await is triggering early. The callback is triggered when <code>managedUpload</code> is resolved/completed. </p>
  
  <p>So is there a way to <code>await</code> the completed object instead of <code>managedUpload</code>?</p>