diff --git a/packages/storage-s3/src/staticHandler.ts b/packages/storage-s3/src/staticHandler.ts index 4c4fd47d68..016ce6ee1b 100644 --- a/packages/storage-s3/src/staticHandler.ts +++ b/packages/storage-s3/src/staticHandler.ts @@ -1,8 +1,8 @@ -import type * as AWS from '@aws-sdk/client-s3' import type { StaticHandler } from '@payloadcms/plugin-cloud-storage/types' import type { CollectionConfig, PayloadRequest } from 'payload' import type { Readable } from 'stream' +import * as AWS from '@aws-sdk/client-s3' import { GetObjectCommand } from '@aws-sdk/client-s3' import { getSignedUrl } from '@aws-sdk/s3-request-presigner' import { getFilePrefix } from '@payloadcms/plugin-cloud-storage/utilities' @@ -94,10 +94,6 @@ export const getHandler = ({ Key: key, }) - if (!object.Body) { - return new Response(null, { status: 404, statusText: 'Not Found' }) - } - const etagFromHeaders = req.headers.get('etag') || req.headers.get('if-none-match') const objectEtag = object.ETag @@ -138,6 +134,9 @@ export const getHandler = ({ status: 200, }) } catch (err) { + if (err instanceof AWS.NoSuchKey) { + return new Response(null, { status: 404, statusText: 'Not Found' }) + } req.payload.logger.error(err) return new Response('Internal Server Error', { status: 500 }) } finally { diff --git a/test/storage-s3/int.spec.ts b/test/storage-s3/int.spec.ts index 60c4a9431e..463724c867 100644 --- a/test/storage-s3/int.spec.ts +++ b/test/storage-s3/int.spec.ts @@ -110,6 +110,11 @@ describe('@payloadcms/storage-s3', () => { expect(response.headers.get('Content-Type')).toBe('image/png') }) + it('should return 404 when the file is not found', async () => { + const response = await restClient.GET(`/${mediaSlug}/file/missing.png`) + expect(response.status).toBe(404) + }) + describe('R2', () => { it.todo('can upload') })