Skip to content

Commit 38f477b

Browse files
authored
fix: delete attachment from s3 (#272)
1 parent d208952 commit 38f477b

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/api/src/routers/attachment.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { generateUID } from "@kan/shared/utils";
99

1010
import { createTRPCRouter, protectedProcedure } from "../trpc";
1111
import { assertUserInWorkspace } from "../utils/auth";
12-
import { generateUploadUrl } from "../utils/s3";
12+
import { deleteObject, generateUploadUrl } from "../utils/s3";
1313

1414
export const attachmentRouter = createTRPCRouter({
1515
generateUploadUrl: protectedProcedure
@@ -189,6 +189,18 @@ export const attachmentRouter = createTRPCRouter({
189189

190190
await assertUserInWorkspace(ctx.db, userId, workspaceId);
191191

192+
const bucket = process.env.NEXT_PUBLIC_ATTACHMENTS_BUCKET_NAME;
193+
if (bucket) {
194+
try {
195+
await deleteObject(bucket, attachment.s3Key);
196+
} catch (error) {
197+
console.error(
198+
`Failed to delete attachment from S3: ${attachment.s3Key}`,
199+
error,
200+
);
201+
}
202+
}
203+
192204
await cardAttachmentRepo.softDelete(ctx.db, {
193205
attachmentId: attachment.id,
194206
deletedAt: new Date(),

packages/api/src/utils/s3.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
DeleteObjectCommand,
23
GetObjectCommand,
34
PutObjectCommand,
45
S3Client,
@@ -56,3 +57,13 @@ export async function generateDownloadUrl(
5657
{ expiresIn },
5758
);
5859
}
60+
61+
export async function deleteObject(bucket: string, key: string) {
62+
const client = createS3Client();
63+
await client.send(
64+
new DeleteObjectCommand({
65+
Bucket: bucket,
66+
Key: key,
67+
}),
68+
);
69+
}

0 commit comments

Comments
 (0)