0

I am downloading a number of items from an Amazon S3 bucket. I am able to download these, but the code which relies on the items being present fails as the downloads haven't completed by the time the following code runs. I have the following for my download:

  const stream = await download.Body?.pipe(createWriteStream(filePath));
  stream.on('finish', async () => {
    console.log(`${filePath} finished downloading`)
  })

and while the file is downloaded, subsequent code runs before the file has been saved. I did see on here (which I can't find) someone claiming that the finish event doesn't work for S3.

I have also tried

await once(stream, 'finish');

but that had the same result. Anyone have any other ideas (or know the event I would need to use?)

Joseph
  • 541
  • 1
  • 4
  • 31
  • Shouldn't the callback in `stream.on()` method be synchronous since you're already waiting for the finish event to happen ? – johnnyBoy Nov 03 '22 at 10:14
  • I am not sure - the documents on this are a bit all over – Joseph Nov 03 '22 at 10:21
  • @johnnyBoy https://stackoverflow.com/questions/11447872/callback-to-handle-completion-of-pipe was one of the examples I was looking at – Joseph Nov 03 '22 at 10:27
  • In the accepted solution, the callback is indeed synchronous you don't need to put the `async` keyword. Try this way and let us know – johnnyBoy Nov 03 '22 at 10:34
  • @johnnyBoy - I did a stream.on('finish', function(){ console.log('file downloaded '); }); but it still fails to wai\ – Joseph Nov 03 '22 at 10:50
  • I looked up the [documentation](https://nodejs.org/en/knowledge/advanced/streams/how-to-use-fs-create-write-stream/) of the `createWriteStream` method, and it seems the event is `end`, not finish. I don’t know any further, but it’s still weird it doesn’t wait. Can you provide a reproduction in a sandbox ? – johnnyBoy Nov 04 '22 at 17:02
  • Hi, it gets very much stranger, I was trying to run this within the playwright framework - seems that it works on it's own, but just playwright is doing something odd. – Joseph Nov 05 '22 at 21:15

0 Answers0