I writing a number of files to AWS S3 using NodeJS. I have the data in a stringifed variable but have also tested reading in a file. The files write successfully to S3 so it does work but the "await Promise.all()" completes before the file uploads complete. I need to know when the files have completed uploading.
// Stream File Data to S3
const data = "blah blah blah"
const s3Path = "my_path"
const promises = []
promises.push(uploadFile(data, `${s3Path}/1111.xml`))
promises.push(uploadFile(data, `${s3Path}/2222.xml`))
promises.push(uploadFile(data, `${s3Path}/3333.xml`))
promises.push(uploadFile(data, `${s3Path}/4444.xml`))
const completed = await Promise.all(promises)
below is the function I'm using to upload the files.
const uploadFile = (data, key) => {
// const fileContent = data
const fileContent = fs.readFileSync("/full_path/test.xml")
// Setting up S3 upload parameters
const params = {
"Bucket": "my_bucket",
"Key": key,
"ContentType": "application/xml",
"Body": fileContent
}
// Uploading files to the bucket
s3.upload(params, (err, data2) => {
if (err) {
throw err
}
console.log(`File uploaded successfully. ${data2.Location}`)
return true
})
}
Is there a way to update this so promise.all() waits for all uploads to completed before progressing?
thankyou