amazon s3 – How to upload files to s3 synchronously using node.js api

I have the following piece of code:

array.forEach(function (item) {

       // *** some processing on each item ***

        var params = {Key: item.id, Body: item.body};
        s3bucket.upload(params, function(err, data) {
            if (err) {
              console.log("Error uploading data. ", err);
            } else {
              console.log("Success uploading data");
        }});
  });

Because s3bucket.upload is being executed asynchronously – the loop finishes before uploading all the items.

How can I force s3bucket.upload to be synchronous?

Meaning don’t jump to next iteration until this item was uploaded (or failed) to S3.

Thanks

Loading...

(Visited 1 times, 1 visits today)