fix(asset): delete entire S3 directory for composite assets to clean up transcodes

This commit is contained in:
yiling
2026-05-24 09:18:58 +08:00
parent 823d915613
commit b6c2c59801
4 changed files with 67 additions and 2 deletions
+20
View File
@@ -51,6 +51,7 @@ export interface S3Service {
getObject(bucket: string, key: string): Promise<S3Object>
downloadToFile(bucket: string, key: string, filePath: string): Promise<void>
deleteObject(bucket: string, key: string): Promise<void>
deletePrefix(bucket: string, prefix: string): Promise<void>
headObject(bucket: string, key: string): Promise<ObjectInfo>
listObjects(bucket: string, prefix: string): Promise<string[]>
uploadFile(filePath: string, contentType: string): Promise<string>
@@ -167,6 +168,13 @@ export class S3StorageService implements S3Service {
await this.client.send(command)
}
async deletePrefix(bucket: string, prefix: string): Promise<void> {
const keys = await this.listObjects(bucket, prefix)
if (keys.length > 0) {
await Promise.all(keys.map((key) => this.deleteObject(bucket, key)))
}
}
async headObject(bucket: string, key: string): Promise<ObjectInfo> {
const command = new HeadObjectCommand({
Bucket: bucket,
@@ -334,6 +342,18 @@ export class LocalStorageService implements S3Service {
}
}
async deletePrefix(bucket: string, prefix: string): Promise<void> {
const dirPath = this.getFilePath(bucket, prefix)
try {
await fs.promises.rm(dirPath, { recursive: true, force: true })
} catch (e: unknown) {
if (e instanceof Error && (e as NodeJS.ErrnoException).code === 'ENOENT') {
return
}
throw e
}
}
async headObject(bucket: string, key: string): Promise<ObjectInfo> {
const filePath = this.getFilePath(bucket, key)
try {