From 687fdda7f731636e26cab48dc594d5a085106652 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Fri, 25 Apr 2025 02:11:08 +0800 Subject: [PATCH] chore: --- packages/plugins/plugin-lib/src/index.ts | 1 + .../plugins/plugin-lib/src/oss/impls/s3.ts | 52 +++++++++++++++---- .../plugin-lib/src/oss/impls/tencentcos.ts | 1 + packages/plugins/plugin-lib/src/s3/index.ts | 1 + 4 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 packages/plugins/plugin-lib/src/s3/index.ts diff --git a/packages/plugins/plugin-lib/src/index.ts b/packages/plugins/plugin-lib/src/index.ts index afd14fe1..ab1d48c1 100644 --- a/packages/plugins/plugin-lib/src/index.ts +++ b/packages/plugins/plugin-lib/src/index.ts @@ -6,3 +6,4 @@ export * from "./tencent/index.js"; export * from "./qiniu/index.js"; export * from "./ctyun/index.js"; export * from "./oss/index.js"; +export * from "./s3/index.js"; diff --git a/packages/plugins/plugin-lib/src/oss/impls/s3.ts b/packages/plugins/plugin-lib/src/oss/impls/s3.ts index ab0f90c7..4e99c881 100644 --- a/packages/plugins/plugin-lib/src/oss/impls/s3.ts +++ b/packages/plugins/plugin-lib/src/oss/impls/s3.ts @@ -1,14 +1,24 @@ import { BaseOssClient, OssClientRemoveByOpts, OssFileItem } from "../api.js"; import path from "node:path"; import { S3Access } from "../../s3/access.js"; +import fs from "fs"; +import dayjs from "dayjs"; export default class S3OssClientImpl extends BaseOssClient { client: any; - + join(...strs: string[]) { + const str = super.join(...strs); + if (str.startsWith("/")) { + return str.substring(1); + } + return str; + } async init() { // import { S3Client } from "@aws-sdk/client-s3"; const { S3Client } = await import("@aws-sdk/client-s3"); this.client = new S3Client({ forcePathStyle: true, + //@ts-ignore + s3ForcePathStyle: true, credentials: { accessKeyId: this.access.accessKeyId, // 默认 MinIO 访问密钥 secretAccessKey: this.access.secretAccessKey, // 默认 MinIO 秘密密钥 @@ -18,16 +28,37 @@ export default class S3OssClientImpl extends BaseOssClient { }); } - download(fileName: string, savePath: string): Promise { - throw new Error("Method not implemented."); + async download(filePath: string, savePath: string): Promise { + const { GetObjectCommand } = await import("@aws-sdk/client-s3"); + const key = path.join(this.rootDir, filePath); + const params = { + Bucket: this.access.bucket, // The name of the bucket. For example, 'sample_bucket_101'. + Key: key, // The name of the object. For example, 'sample_upload.txt'. + }; + const res = await this.client.send(new GetObjectCommand({ ...params })); + const fileContent = fs.createWriteStream(savePath); + res.Body.pipe(fileContent); + + this.logger.info(`文件下载成功: ${savePath}`); } - removeBy(removeByOpts: OssClientRemoveByOpts): Promise { - throw new Error("Method not implemented."); + + async listDir(dir: string): Promise { + const { ListObjectsCommand } = await import("@aws-sdk/client-s3"); + const dirKey = this.join(this.rootDir, dir); + const params = { + Bucket: this.access.bucket, // The name of the bucket. For example, 'sample_bucket_101'. + Prefix: dirKey, // The name of the object. For example, 'sample_upload.txt'. + }; + const res = await this.client.send(new ListObjectsCommand({ ...params })); + return res.Contents.map(item => { + return { + path: item.Key, + size: item.Size, + lastModified: dayjs(item.LastModified).valueOf(), + }; + }); } - listDir(dir: string): Promise { - throw new Error("Method not implemented."); - } - async upload(filePath: string, fileContent: Buffer) { + async upload(filePath: string, fileContent: Buffer | string) { const { PutObjectCommand } = await import("@aws-sdk/client-s3"); const key = path.join(this.rootDir, filePath); this.logger.info(`开始上传文件: ${key}`); @@ -35,6 +66,9 @@ export default class S3OssClientImpl extends BaseOssClient { Bucket: this.access.bucket, // The name of the bucket. For example, 'sample_bucket_101'. Key: key, // The name of the object. For example, 'sample_upload.txt'. }; + if (typeof fileContent === "string") { + fileContent = fs.createReadStream(fileContent) as any; + } await this.client.send(new PutObjectCommand({ Body: fileContent, ...params })); this.logger.info(`文件上传成功: ${filePath}`); diff --git a/packages/plugins/plugin-lib/src/oss/impls/tencentcos.ts b/packages/plugins/plugin-lib/src/oss/impls/tencentcos.ts index 20fe9be2..eaad9225 100644 --- a/packages/plugins/plugin-lib/src/oss/impls/tencentcos.ts +++ b/packages/plugins/plugin-lib/src/oss/impls/tencentcos.ts @@ -28,6 +28,7 @@ export default class TencentOssClientImpl extends BaseOssClient { const dirKey = this.join(this.rootDir, dir) + "/"; + // @ts-ignore const res: any[] = await this.client.listDir(dirKey); return res.map(item => { return { diff --git a/packages/plugins/plugin-lib/src/s3/index.ts b/packages/plugins/plugin-lib/src/s3/index.ts new file mode 100644 index 00000000..655094d0 --- /dev/null +++ b/packages/plugins/plugin-lib/src/s3/index.ts @@ -0,0 +1 @@ +export * from "./access.js";