aws / aws-sdk-js

AWS SDK for JavaScript in the browser and Node.js
https://aws.amazon.com/developer/language/javascript/
Apache License 2.0
7.59k stars 1.55k forks source link

send invalid key to s3.getObject will crash the nodejs backend(I use it in nestjs). #3846

Closed Vuinfang closed 2 years ago

Vuinfang commented 3 years ago

1056 is closed but I got the same error, and this error will make the whole backend no response, I cannot catch this error, it also not shows in the callback.

ajredniwja commented 2 years ago

Hey @Vuinfang, thanks for opening this issue, can you please share a reproducible code?

github-actions[bot] commented 2 years ago

This issue has not received a response in 1 week. If you still think there is a problem, please leave a comment to avoid the issue from automatically closing.

LuisOfCourse commented 2 years ago

Hi @ajredniwja, I have got the same problem but with every call that I purposely try to fail. I was trying to catch the error when a wrong key was inserted (getObject) but I noticed that everything that throws this repo is crashing the entire server. Can you please reopen? Ill share a quick example, but it is easy to recreate.

controller @Get('test/:test') async getFiles(@Param() { test }, @Res() res: Response) { const file = await this.filesService.getFiles(test); file.createReadStream().pipe(res) }

service async getFiles(test) { return await this.fileAwsS3Service.downloadS3( 'test/' + test + 'file.txt', ); }

aws s3 service

downloadS3(filePath: string) {
  const s3 = this.getS3();
  const params = {
    Bucket: this.configService.get('aws.s3.bucket'),
    Key: filePath,
  };
  return s3.getObject(params);
}

getS3() {
  return new S3({
    region: this.configService.get('aws.s3.region'),
    accessKeyId: this.configService.get('aws.s3.accessKeyID'),
    secretAccessKey: this.configService.get('aws.s3.secret'),
  });
}

If I try to purposly mess up the url/key I get 'key doesn't exists' but the application crashes. This example is the clean code with no try catch nor handlers.