feat(project): implement async project deletion and storage cleanup

This commit is contained in:
yiling
2026-05-23 18:09:30 +08:00
parent b9bb4ba143
commit 8bc275e446
9 changed files with 408 additions and 22 deletions
+23
View File
@@ -4,6 +4,7 @@ import {
HeadObjectCommand,
ListObjectsV2Command,
PutObjectCommand,
DeleteObjectCommand,
S3Client,
} from '@aws-sdk/client-s3'
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
@@ -49,6 +50,7 @@ export interface S3Service {
): Promise<void>
getObject(bucket: string, key: string): Promise<S3Object>
downloadToFile(bucket: string, key: string, filePath: string): Promise<void>
deleteObject(bucket: string, key: string): Promise<void>
headObject(bucket: string, key: string): Promise<ObjectInfo>
listObjects(bucket: string, prefix: string): Promise<string[]>
uploadFile(filePath: string, contentType: string): Promise<string>
@@ -157,6 +159,14 @@ export class S3StorageService implements S3Service {
})
}
async deleteObject(bucket: string, key: string): Promise<void> {
const command = new DeleteObjectCommand({
Bucket: bucket,
Key: key,
})
await this.client.send(command)
}
async headObject(bucket: string, key: string): Promise<ObjectInfo> {
const command = new HeadObjectCommand({
Bucket: bucket,
@@ -311,6 +321,19 @@ export class LocalStorageService implements S3Service {
await fs.promises.copyFile(srcPath, filePath)
}
async deleteObject(bucket: string, key: string): Promise<void> {
const filePath = this.getFilePath(bucket, key)
try {
await fs.promises.unlink(filePath)
} catch (e: unknown) {
if (e instanceof Error && (e as NodeJS.ErrnoException).code === 'ENOENT') {
// Ignore if file doesn't exist
return
}
throw e
}
}
async headObject(bucket: string, key: string): Promise<ObjectInfo> {
const filePath = this.getFilePath(bucket, key)
try {