From b7113bda2378116d6c116dc583f563cce7cf9f00 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Fri, 25 Apr 2025 18:36:49 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=B8=83=E7=89=9Boss=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=BF=87=E6=9C=9F=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/acme-client/types/index.test-d.js | 4 +- .../plugin-lib/src/oss/impls/qiniuoss.ts | 20 ++++--- .../plugins/plugin-lib/src/oss/impls/s3.ts | 5 ++ .../plugins/plugin-lib/src/qiniu/lib/sdk.ts | 58 +++++++++++++++---- .../plugin-lib/src/tencent/lib/cos-client.ts | 2 +- 5 files changed, 69 insertions(+), 20 deletions(-) diff --git a/packages/core/acme-client/types/index.test-d.js b/packages/core/acme-client/types/index.test-d.js index 58a1822b..75770dd3 100644 --- a/packages/core/acme-client/types/index.test-d.js +++ b/packages/core/acme-client/types/index.test-d.js @@ -12,8 +12,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; var __generator = (this && this.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); + return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); diff --git a/packages/plugins/plugin-lib/src/oss/impls/qiniuoss.ts b/packages/plugins/plugin-lib/src/oss/impls/qiniuoss.ts index 53d0eb15..0f65acaf 100644 --- a/packages/plugins/plugin-lib/src/oss/impls/qiniuoss.ts +++ b/packages/plugins/plugin-lib/src/oss/impls/qiniuoss.ts @@ -12,14 +12,20 @@ export default class QiniuOssClientImpl extends BaseOssClient { }); } - download(fileName: string, savePath: string): Promise { - throw new Error("Method not implemented."); + async download(fileName: string, savePath: string): Promise { + const path = this.join(this.rootDir, fileName); + await this.client.downloadFile(this.access.bucket, path, savePath); } - removeBy(removeByOpts: OssClientRemoveByOpts): Promise { - throw new Error("Method not implemented."); - } - listDir(dir: string): Promise { - throw new Error("Method not implemented."); + async listDir(dir: string): Promise { + const path = this.join(this.rootDir, dir); + const res = await this.client.listDir(this.access.bucket, path); + return res.items.map(item => { + return { + path: item.name, + size: item.fsize, + lastModified: item.putTime, + }; + }); } async upload(filePath: string, fileContent: Buffer) { const path = this.join(this.rootDir, filePath); diff --git a/packages/plugins/plugin-lib/src/oss/impls/s3.ts b/packages/plugins/plugin-lib/src/oss/impls/s3.ts index 642e667f..f197562e 100644 --- a/packages/plugins/plugin-lib/src/oss/impls/s3.ts +++ b/packages/plugins/plugin-lib/src/oss/impls/s3.ts @@ -15,6 +15,7 @@ export default class S3OssClientImpl extends BaseOssClient { } async init() { // import { S3Client } from "@aws-sdk/client-s3"; + //@ts-ignore const { S3Client } = await import("@aws-sdk/client-s3"); this.client = new S3Client({ forcePathStyle: true, @@ -30,6 +31,7 @@ export default class S3OssClientImpl extends BaseOssClient { } async download(filePath: string, savePath: string): Promise { + // @ts-ignore const { GetObjectCommand } = await import("@aws-sdk/client-s3"); const key = path.join(this.rootDir, filePath); const params = { @@ -44,6 +46,7 @@ export default class S3OssClientImpl extends BaseOssClient { } async listDir(dir: string): Promise { + // @ts-ignore const { ListObjectsCommand } = await import("@aws-sdk/client-s3"); const dirKey = this.join(this.rootDir, dir); const params = { @@ -60,6 +63,7 @@ export default class S3OssClientImpl extends BaseOssClient { }); } async upload(filePath: string, fileContent: Buffer | string) { + // @ts-ignore const { PutObjectCommand } = await import("@aws-sdk/client-s3"); const key = path.join(this.rootDir, filePath); this.logger.info(`开始上传文件: ${key}`); @@ -77,6 +81,7 @@ export default class S3OssClientImpl extends BaseOssClient { async remove(filePath: string) { const key = path.join(this.rootDir, filePath); + // @ts-ignore const { DeleteObjectCommand } = await import("@aws-sdk/client-s3"); await this.client.send( new DeleteObjectCommand({ diff --git a/packages/plugins/plugin-lib/src/qiniu/lib/sdk.ts b/packages/plugins/plugin-lib/src/qiniu/lib/sdk.ts index 393838fe..39a22946 100644 --- a/packages/plugins/plugin-lib/src/qiniu/lib/sdk.ts +++ b/packages/plugins/plugin-lib/src/qiniu/lib/sdk.ts @@ -1,5 +1,6 @@ -import { HttpClient, ILogger } from "@certd/basic"; +import { HttpClient, ILogger, utils } from "@certd/basic"; import { QiniuAccess } from "../access.js"; +import fs from "fs"; export type QiniuCertInfo = { key: string; @@ -98,7 +99,7 @@ export class QiniuClient { }); } - async uploadFile(bucket: string, key: string, content: Buffer) { + async uploadFile(bucket: string, key: string, content: Buffer | string) { const sdk = await import("qiniu"); const qiniu = sdk.default; const mac = new qiniu.auth.digest.Mac(this.access.accessKey, this.access.secretKey); @@ -111,8 +112,15 @@ export class QiniuClient { const config = new qiniu.conf.Config(); const formUploader = new qiniu.form_up.FormUploader(config); const putExtra = new qiniu.form_up.PutExtra(); - // 文件上传 - const { data, resp } = await formUploader.put(uploadToken, key, content, putExtra); + let res: any = {}; + if (typeof content === "string") { + const readableStream = fs.createReadStream(content); + res = await formUploader.putStream(uploadToken, key, readableStream, putExtra); + } else { + // 文件上传 + res = await formUploader.put(uploadToken, key, content, putExtra); + } + const { data, resp } = res; if (resp.statusCode === 200) { this.logger.info("文件上传成功:" + key); return data; @@ -123,12 +131,7 @@ export class QiniuClient { } async removeFile(bucket: string, key: string) { - const sdk = await import("qiniu"); - const qiniu = sdk.default; - const mac = new qiniu.auth.digest.Mac(this.access.accessKey, this.access.secretKey); - const config = new qiniu.conf.Config(); - config.useHttpsDomain = true; - const bucketManager = new qiniu.rs.BucketManager(mac, config); + const bucketManager = await this.getBucketManager(); const { resp } = await bucketManager.delete(bucket, key); @@ -139,4 +142,39 @@ export class QiniuClient { throw new Error("删除失败:" + JSON.stringify(resp)); } } + + async downloadFile(bucket: string, path: string, savePath: string) { + const bucketManager = await this.getBucketManager(); + const privateBucketDomain = `http://${bucket}.qiniudn.com`; + const deadline = Math.floor(Date.now() / 1000) + 3600; // 1小时过期 + const privateDownloadUrl = bucketManager.privateDownloadUrl(privateBucketDomain, path, deadline); + + await utils.request.download({ + http: this.http, + logger: this.logger, + config: { + url: privateDownloadUrl, + method: "get", + }, + savePath, + }); + } + + private async getBucketManager() { + const sdk = await import("qiniu"); + const qiniu = sdk.default; + const mac = new qiniu.auth.digest.Mac(this.access.accessKey, this.access.secretKey); + const config = new qiniu.conf.Config(); + config.useHttpsDomain = true; + return new qiniu.rs.BucketManager(mac, config); + } + + async listDir(bucket: string, path: string) { + const bucketManager = await this.getBucketManager(); + const res = await bucketManager.listPrefix(bucket, { + prefix: path, + limit: 1000, + }); + return res.data; + } } diff --git a/packages/plugins/plugin-lib/src/tencent/lib/cos-client.ts b/packages/plugins/plugin-lib/src/tencent/lib/cos-client.ts index 25e576b6..77214bcc 100644 --- a/packages/plugins/plugin-lib/src/tencent/lib/cos-client.ts +++ b/packages/plugins/plugin-lib/src/tencent/lib/cos-client.ts @@ -24,7 +24,7 @@ export class TencentCosClient { return new sdk.default(clientConfig); } - async uploadFile(key: string, file: Buffer) { + async uploadFile(key: string, file: Buffer | string) { const cos = await this.getCosClient(); return new Promise((resolve, reject) => { cos.putObject(