feat(asset): add move to and copy to functionalities

This commit is contained in:
yiling
2026-05-24 20:15:55 +08:00
parent ffba9940e2
commit c5fba746be
9 changed files with 687 additions and 5 deletions
+37
View File
@@ -1,5 +1,6 @@
import { ObjectInfo } from '@/dtos/s3'
import {
CopyObjectCommand,
DeleteObjectCommand,
GetObjectCommand,
HeadObjectCommand,
@@ -49,6 +50,12 @@ export interface S3Service {
contentType?: string,
): Promise<void>
getObject(bucket: string, key: string): Promise<S3Object>
copyObject(
sourceBucket: string,
sourceKey: string,
destBucket: string,
destKey: string,
): Promise<void>
downloadToFile(bucket: string, key: string, filePath: string): Promise<void>
deleteObject(bucket: string, key: string): Promise<number>
deletePrefix(bucket: string, prefix: string): Promise<number>
@@ -131,6 +138,21 @@ export class S3StorageService implements S3Service {
}
}
async copyObject(
sourceBucket: string,
sourceKey: string,
destBucket: string,
destKey: string,
): Promise<void> {
const command = new CopyObjectCommand({
Bucket: destBucket,
Key: destKey,
// eslint-disable-next-line @typescript-eslint/naming-convention
CopySource: `${sourceBucket}/${sourceKey}`,
})
await this.client.send(command)
}
async downloadToFile(bucket: string, key: string, filePath: string): Promise<void> {
const command = new GetObjectCommand({
Bucket: bucket,
@@ -331,6 +353,21 @@ export class LocalStorageService implements S3Service {
}
}
async copyObject(
sourceBucket: string,
sourceKey: string,
destBucket: string,
destKey: string,
): Promise<void> {
const srcPath = this.getFilePath(sourceBucket, sourceKey)
const destPath = this.getFilePath(destBucket, destKey)
const dir = path.dirname(destPath)
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
await fs.promises.copyFile(srcPath, destPath)
}
async downloadToFile(bucket: string, key: string, filePath: string): Promise<void> {
const srcPath = this.getFilePath(bucket, key)
await fs.promises.copyFile(srcPath, filePath)