fix: NoSuchKey, The specified key does not exist.

This commit is contained in:
Rotimi Best
2026-06-07 23:47:14 +01:00
parent d7225a77b2
commit 5f07184b29
4 changed files with 111 additions and 10 deletions
+19 -1
View File
@@ -1,4 +1,4 @@
import { GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3';
import { GetObjectCommand, HeadObjectCommand, NotFound, PutObjectCommand } from '@aws-sdk/client-s3';
import { Readable } from 'node:stream';
import { createWriteStream } from 'node:fs';
import { mkdir, readFile, unlink } from 'node:fs/promises';
@@ -13,6 +13,24 @@ import { getS3Client, getStorageConfig } from '../config/storage';
* Workers prefer disk over buffers so ffmpeg can seek without holding the
* whole video in RAM.
*/
export async function objectExists(bucket: string, key: string): Promise<boolean> {
try {
await getS3Client().send(new HeadObjectCommand({ Bucket: bucket, Key: key }));
return true;
} catch (error) {
if (error instanceof NotFound || (error as { name?: string }).name === 'NotFound') {
return false;
}
const status = (error as { $metadata?: { httpStatusCode?: number } }).$metadata?.httpStatusCode;
if (status === 404) {
return false;
}
throw error;
}
}
export async function downloadObjectToTempFile(bucket: string, key: string, fileNameHint?: string): Promise<string> {
const dir = path.join(tmpdir(), 'cio-jobs');
await mkdir(dir, { recursive: true });