Skip to content

Commit bebdfba

Browse files
committed
Throw 404 if s3 object is not found
Fixes: #15
1 parent 467e780 commit bebdfba

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

apps/api/src/images/images.controller.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Controller, Get, Header, Param, Redirect, Res } from '@nestjs/common';
1+
import { Controller, Get, Header, NotFoundException, Param, Redirect, Res } from '@nestjs/common';
22
import { Response } from 'express';
33
import { ImagesService } from './images.service';
44

@@ -18,6 +18,9 @@ export class ImagesController {
1818
@Header('Cache-Control', `public, max-age=${CACHE_TIME}`)
1919
async getByKey(@Param('key') key: string, @Res() response: Response) {
2020
const object = await this.imagesService.getObject(key);
21+
if (!object) {
22+
throw new NotFoundException('Object with provided key was not found');
23+
}
2124
object.pipe(response);
2225
}
2326
}

apps/api/src/s3/s3.service.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
DeleteObjectCommandInput,
33
GetObjectCommandInput,
4+
NoSuchKey,
45
PutObjectCommandInput,
56
} from '@aws-sdk/client-s3';
67
import { Injectable, Logger } from '@nestjs/common';
@@ -37,7 +38,13 @@ export class S3Service {
3738
Key: key,
3839
};
3940

40-
const object = await this.s3.getObject(params);
41+
const object = await this.s3.getObject(params).catch((error) => {
42+
if (error instanceof NoSuchKey) {
43+
return null;
44+
}
45+
throw error;
46+
});
47+
if (object == null) return null;
4148
const body = object.Body as Readable;
4249
return body;
4350
}

0 commit comments

Comments
 (0)