Skip to content

Commit 3451754

Browse files
committed
feat: added new s3FileExists function
1 parent b5b3287 commit 3451754

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/S3Management.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
PutObjectCommand,
88
GetObjectCommand,
99
ListObjectsV2Command,
10+
HeadObjectCommand,
1011
} from "@aws-sdk/client-s3";
1112
import { Readable } from "stream";
1213

@@ -103,6 +104,19 @@ export function getListObjectsFromS3(bucket: string, prefix: string, maxKeys?: n
103104
return stream;
104105
}
105106

107+
export async function s3FileExists(bucket: string, key: string, s3Client?: S3Client) {
108+
s3Client ??= getS3Client();
109+
try {
110+
await s3Client.send(
111+
new HeadObjectCommand({ Bucket: bucket, Key: key })
112+
);
113+
return true;
114+
} catch (err) {
115+
if (err instanceof Error && err.name === "NotFound") return false;
116+
throw err;
117+
}
118+
}
119+
106120
// For tests
107121
export function resetS3Client() {
108122
s3Client = undefined;

0 commit comments

Comments
 (0)