I am trying to get data from a file from s3 bucket and write to a temp file, then read the data from that temp file. The file was successfully created and data which is some html shown in the temp file, but when I try to console.log the next step, it return empty.
try {
...
const params = {Bucket: "somebucket", Key: "file.html"}
let tempFile = fs.createWriteStream("./temp/file.html", 'utf8')
s3.getObject(params).createReadStream().pipe(tempFile)
fs.readFile('./temp/file.html', 'utf8', (err, data) => {
if (err) console.log(err)
else {
console.log("cannot get data?") //this shows up
console.log(data) // this one does not shows up
}
})
...
} catch {
...
}
Anyone have experience in this?
Update:
I realize the data is inside the file and can be output fine without the getObject() part, but the data cannot be display out. It might be due to the file being read before the data is fully inserted to the file. How should i change the code so I can delay the readFile to execute after the data inserted in the file is complete. I tried setTimeout but failed.