commit 5563ded0a19d60c34cce6272e272c6405bcab304 Author: xion Date: Sat Mar 22 19:47:23 2025 +0800 ini diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f06235c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +dist diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..a5aa07b --- /dev/null +++ b/.npmrc @@ -0,0 +1,2 @@ +//npm.xiongxiao.me/:_authToken=${ME_NPM_TOKEN} +//registry.npmjs.org/:_authToken=${NPM_TOKEN} diff --git a/package.json b/package.json new file mode 100644 index 0000000..ec6b0f7 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "@kevisual/oss", + "version": "0.0.1", + "description": "", + "main": "index.js", + "scripts": { + "build": "tsup", + "dev": "tsup --watch", + "dev:lib": "tsup --watch" + }, + "files": [ + "dist" + ], + "keywords": [], + "author": "abearxiong ", + "license": "MIT", + "type": "module", + "devDependencies": { + "minio": "^8.0.5", + "tsup": "^8.4.0" + } +} \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..38046d9 --- /dev/null +++ b/readme.md @@ -0,0 +1,9 @@ +# oss 管理,对minio的封装 + +一般常用的功能模块。 + +- 上传文件 +- 下载文件 +- 删除文件 +- 复制文件 +- 列出文件 diff --git a/src/core/copy-object.ts b/src/core/copy-object.ts new file mode 100644 index 0000000..bd397b6 --- /dev/null +++ b/src/core/copy-object.ts @@ -0,0 +1,24 @@ +import { Client, CopyDestinationOptions, CopySourceOptions } from 'minio'; + +type CopyObjectOpts = { + bucketName: string; + newMetadata: Record; + prefix: string; + client: Client; +}; +/** + * 复制对象 REPLACE 替换 + * @param param0 + * @returns + */ +export const copyObject = async ({ bucketName, newMetadata, prefix, client }: CopyObjectOpts) => { + const source = new CopySourceOptions({ Bucket: bucketName, Object: prefix }); + const destination = new CopyDestinationOptions({ + Bucket: bucketName, + Object: prefix, + UserMetadata: newMetadata, + MetadataDirective: 'REPLACE', + }); + const copyResult = await client.copyObject(source, destination); + return copyResult; +}; diff --git a/src/core/type.ts b/src/core/type.ts new file mode 100644 index 0000000..dd9fe0b --- /dev/null +++ b/src/core/type.ts @@ -0,0 +1,49 @@ +import { ItemBucketMetadata, Client } from 'minio'; +type UploadedObjectInfo = { + etag: string; + lastModified: Date; + size: number; + versionId: string; + metadata: ItemBucketMetadata; +}; + +export interface OssBaseOperation { + /** + * 获取对象 + * @param objectName 对象名 + */ + getObject(objectName: string): Promise; + /** + * 上传对象 + * @param objectName 对象名 + * @param data 数据 + */ + putObject(objectName: string, data: Buffer | string, metaData?: ItemBucketMetadata): Promise; + /** + * 上传文件 + * @param objectName 对象名 + * @param filePath 文件路径 + */ + fPutObject(objectName: string, filePath: string, metaData?: ItemBucketMetadata): Promise; + /** + * 获取对象信息 + * @param objectName 对象名 + */ + statObject(objectName: string): Promise; + /** + * 删除对象 + * @param objectName 对象名 + */ + deleteObject(objectName: string): Promise; + /** + * 列出对象 + * @param prefix 前缀 + */ + listObjects(prefix: string, opts?: { recursive?: boolean }): Promise; + /** + * 复制对象 + * @param sourceObject 源对象 + * @param targetObject 目标对象 + */ + copyObject: Client['copyObject']; +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..7f123c9 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,59 @@ +import { Client, ItemBucketMetadata } from 'minio'; +import { OssBaseOperation } from './core/type.ts'; +type OssBaseOptions = { + /** + * 已经初始化好的minio client + */ + client?: Client; + /** + * 桶名 + */ + bucketName: string; +}; +export class OssBase implements OssBaseOperation { + client?: Client; + bucketName: string; + constructor(opts: OssBaseOptions) { + if (!opts.client) { + throw new Error('client is required'); + } + this.bucketName = opts.bucketName; + this.client = opts.client; + } + async getObject(objectName: string) { + const bucketName = this.bucketName; + const obj = await this.client.getObject(bucketName, objectName); + return obj; + } + async putObject(objectName: string, data: Buffer | string, metaData?: ItemBucketMetadata) { + const size = data.length; + const bucketName = this.bucketName; + const obj = await this.client.putObject(bucketName, objectName, data, size, metaData); + return obj as any; + } + async deleteObject(objectName: string) { + const bucketName = this.bucketName; + const obj = await this.client.removeObject(bucketName, objectName); + return obj; + } + async listObjects(prefix: string) { + const bucketName = this.bucketName; + const obj = this.client.listObjects(bucketName, prefix); + return obj; + } + async fPutObject(objectName: string, filePath: string, metaData?: ItemBucketMetadata) { + const bucketName = this.bucketName; + const obj = await this.client.fPutObject(bucketName, objectName, filePath, metaData); + return obj as any; + } + async statObject(objectName: string) { + const bucketName = this.bucketName; + const obj = await this.client.statObject(bucketName, objectName); + return obj; + } + async copyObject(sourceObject: any, targetObject: any) { + const bucketName = this.bucketName; + const obj = await this.client.copyObject(bucketName, sourceObject, targetObject); + return obj; + } +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..4be3d26 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,32 @@ +{ + "compilerOptions": { + "module": "nodenext", + "target": "esnext", + "noImplicitAny": false, + "outDir": "./dist", + "sourceMap": false, + "allowJs": true, + "newLine": "LF", + "baseUrl": "./", + "typeRoots": [ + "node_modules/@types", + "//node_modules/@kevisual/types" + ], + "declaration": true, + "noEmit": false, + "allowImportingTsExtensions": true, + "emitDeclarationOnly": true, + "moduleResolution": "NodeNext", + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "esModuleInterop": true, + "paths": { + "@/*": [ + "src/*" + ] + } + }, + "include": [ + "src/**/*.ts", + ], +} \ No newline at end of file diff --git a/tsup.config.ts b/tsup.config.ts new file mode 100644 index 0000000..a277145 --- /dev/null +++ b/tsup.config.ts @@ -0,0 +1,14 @@ +import { defineConfig } from 'tsup'; + +export default defineConfig({ + entry: ['src/index.ts'], + + splitting: false, + sourcemap: false, + clean: true, + format: 'esm', + external: ['minio'], + dts: true, + outDir: 'dist', + tsconfig: 'tsconfig.json', +});