Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading multiple files at same time from local to s3 bucket through node js

I am trying to upload a files to the s3 bucket,The following code I am using to accomplish this operation.

var params = {
  localFile: "../Processor/1.wav",

  s3Params: {
    Bucket: "bucketname",
    Key: "1.wav",
  },
};
var uploader = client.uploadFile(params);
uploader.on('error', function(err) {
  console.error("unable to upload:", err.stack);
});
uploader.on('progress', function() {
  console.log("progress", uploader.progressMd5Amount,
            uploader.progressAmount, uploader.progressTotal);
});
uploader.on('end', function() {
  console.log("done uploading");
});

Every thing works fine till this point. Now lets say here, I want to upload 5 files from local path to s3 bucket, How can I achieve that is there any direct method providing amazon for multiple file uploads or I need to use async module.?

like image 802
Kishore Indraganti Avatar asked Aug 31 '15 10:08

Kishore Indraganti


People also ask

Are multipart uploads faster?

Multipart Upload is generally faster because it can make full use of your available upload bandwidth.


1 Answers

The NodeJS AWS SDK doesn't have any bulk S3 upload method, I'd suggest you to use async/await to upload multiple files at once. Here's an example:

const s3 = new AWS.S3()
const params = [
  { Bucket: 'bucket', Key: 'key', Body: 'body' },
  { Bucket: 'bucket', Key: 'key2', Body: 'body2' },
  { Bucket: 'bucket', Key: 'key3', Body: 'body3' }
]
const responses = await Promise.all(
  params.map(param => s3.upload(param).promise())
)
console.log(responses)
like image 109
Scaccoman Avatar answered Sep 20 '22 01:09

Scaccoman