s3.getObject().createReadStream() : How to catch the error?

前端 未结 3 655
忘了有多久
忘了有多久 2021-01-03 18:29

I am trying to write a program to get a zip file from s3, unzip it, then upload it to S3. But I found two exceptions that I can not catch.

1.

3条回答
  •  粉色の甜心
    2021-01-03 18:50

    You need to listen for the emitted error earlier. Your error handler is only looking for errors during the unzip part.

    A simplified version of your script.

    s3.getObject(params)
    .createReadStream()
    .on('error', (e) => {
      // handle aws s3 error from createReadStream
    })
    .pipe(unzip)
    .on('data', (data) => {
      // retrieve data
    })
    .on('end', () => {
      // stream has ended
    })
    .on('error', (e) => {
      // handle error from unzip
    });
    

    This way, you do not need to make an additional call to AWS to find out if out if it exists.

提交回复
热议问题