Commit 741d633a authored by nanahira's avatar nanahira

Update console-api/src/s3/s3.service.ts

parent ccbcaee7
Pipeline #39020 passed with stages
in 1 minute and 34 seconds
...@@ -66,14 +66,14 @@ export class S3Service extends ConsoleLogger { ...@@ -66,14 +66,14 @@ export class S3Service extends ConsoleLogger {
async removeObjects(paths: string[]) { async removeObjects(paths: string[]) {
const CHUNK_SIZE = 1000; const CHUNK_SIZE = 1000;
// 按 1000 一组分割 // 分块
const chunks = []; const chunks: string[][] = [];
for (let i = 0; i < paths.length; i += CHUNK_SIZE) { for (let i = 0; i < paths.length; i += CHUNK_SIZE) {
chunks.push(paths.slice(i, i + CHUNK_SIZE)); chunks.push(paths.slice(i, i + CHUNK_SIZE));
} }
// 顺序执行每组删除 // 构造所有 delete 命令的 promise
for (const chunk of chunks) { const deletePromises = chunks.map((chunk) => {
const command = new DeleteObjectsCommand({ const command = new DeleteObjectsCommand({
Bucket: this.bucket, Bucket: this.bucket,
Delete: { Delete: {
...@@ -83,12 +83,13 @@ async removeObjects(paths: string[]) { ...@@ -83,12 +83,13 @@ async removeObjects(paths: string[]) {
}, },
}); });
try { return this.s3.send(command).catch((e) => {
await this.s3.send(command);
} catch(e) {
this.error(`Failed to remove ${chunk.length} S3 objects: ${e}`); this.error(`Failed to remove ${chunk.length} S3 objects: ${e}`);
} });
} });
// 并发执行全部请求
await Promise.all(deletePromises);
} }
async fileExists(path: string) { async fileExists(path: string) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment