I'm new to Nodejs. I am saving a zip file from S3 bucket to EC2 instance which is hosting the nodejs service, and then I am decompressing the zip file locally in the file system in EC2:
const s3Item = await S3.getFile(objectBucket, objectKey);
let stream = s3Item.Body.pipe(fs.createWriteStream(sourceFilePath, { mode: 0o777 }));
stream.on('finish', () => {
logger.info(` pipe done `);
});
logger.info(`start decompressing...`);
await decompressArchive(sourceFilePath, targetDirectory_decompressed);
... a lot of code...
However, start decompressing...
is always printed before pipe done
is printed.
How can I make these two steps synchronous such that we wait until pipe done
, and then start to decompress?
I want to use async/await , since I simply cannot put all the rest of the code in the stream.on('finish', () => {});
block, since there are too much code and they all rely on the stream being finished.
I have searched for related answers (there are many of them), but I still cannot get it working.
await new Promise((resolve, reject) => {})
method just now. This is cool. New lesson learnt :) – Fabriane