My image route uses a controller like this:
public getImage(request: Request, response: Response): Response {
try {
const key = request.params.key;
const read = getFileStream(key);
return read.pipe(response);
} catch (error) {
return response.status(404).json({
message: 'Image not found.'
});
}
}
Additionally, I have the following function:
// aws.ts
export function getFileStream(fileKey: any) {
const downloadParams = {
Key: fileKey,
Bucket: bucketName
};
return s3.getObject(downloadParams).createReadStream();
}
However, I encounter an issue when I provide a key that doesn't exist in the S3 bucket. The try/catch block does not handle this scenario, causing my app to crash with an 'Access denied' error code. How can I resolve this situation? Thank you! 😁