Skip to content

Commit

Permalink
Add s3 file exist function
Browse files Browse the repository at this point in the history
  • Loading branch information
constantgillet committed Apr 25, 2024
1 parent 2547c99 commit 14f8c90
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/libs/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type PutObjectCommandInput,
DeleteObjectCommand,
DeleteObjectsCommand,
HeadObjectCommand,
} from "@aws-sdk/client-s3";

const s3 = new S3Client({
Expand Down Expand Up @@ -86,3 +87,23 @@ export const multipleDeleteFromS3 = async (keys: string[]) => {
throw new Error("Error deleting files from S3");
}
};

export const fileExists = async (filePath: string) => {
const command = new HeadObjectCommand({
Bucket: "cgbucket",
Key: filePath,
});

try {
await s3.send(command);
console.log(`File exists: ${filePath}`);
return { exists: true, error: null };
} catch (error) {
if (error.name === "NotFound") {
console.log(`File does not exist: ${filePath}`);
return { exists: false, error: null };
}
console.error(`Error checking file existence: ${error}`);
return { exists: false, error };
}
};

0 comments on commit 14f8c90

Please sign in to comment.