8
votes

I'm trying to upload multiple files to a Google Cloud Storage bucket using NodeJS. I want all files to be uploaded before continuing. I tried several approaches but I can't seem to get it right.

const jpegImages = await fs.readdir(jpegFolder);

console.log('start uploading');

await jpegImages.forEach(async fileName => {
    await bucket.upload(
        path.join(jpegFolder, fileName),
        {destination: fileName}
     ).then( () => {
         console.log(fileName + ' uploaded');
     })
})

console.log('finished uploading');

This gives me the following output, which is not what I expect. Why is the 'finished uploading' log not executed after uploading the files?

start uploading
finished uploading
image1.jpeg uploaded
image2.jpeg uploaded
image3.jpeg uploaded
1

1 Answers

12
votes

async/await doesn't work with forEach and other array methods.

If you don't need sequential uploading (files can be uploaded in parallel) you could create an array of Promises and use Promise.all() to execute them all at once.

const jpegImages = await fs.readdir(jpegFolder);

console.log('start uploading');

await Promise
    .all(jpegImages.map(fileName => {
        return bucket.upload(path.join(jpegFolder, fileName), {destination: fileName})
    }))
    .then(() => {
        console.log('All images uploaded')
    })
    .catch(error => {
        console.error(`Error occured during images uploading: ${error}`);
    });

console.log('finished uploading');