ini
This commit is contained in:
		
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | |||||||
|  | node_modules | ||||||
|  | dist | ||||||
							
								
								
									
										2
									
								
								.npmrc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								.npmrc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | |||||||
|  | //npm.xiongxiao.me/:_authToken=${ME_NPM_TOKEN} | ||||||
|  | //registry.npmjs.org/:_authToken=${NPM_TOKEN} | ||||||
							
								
								
									
										22
									
								
								package.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								package.json
									
									
									
									
									
										Normal file
									
								
							| @@ -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 <xiongxiao@xiongxiao.me>", | ||||||
|  |   "license": "MIT", | ||||||
|  |   "type": "module", | ||||||
|  |   "devDependencies": { | ||||||
|  |     "minio": "^8.0.5", | ||||||
|  |     "tsup": "^8.4.0" | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										9
									
								
								readme.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								readme.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | # oss 管理,对minio的封装 | ||||||
|  |  | ||||||
|  | 一般常用的功能模块。 | ||||||
|  |  | ||||||
|  | - 上传文件 | ||||||
|  | - 下载文件 | ||||||
|  | - 删除文件 | ||||||
|  | - 复制文件 | ||||||
|  | - 列出文件 | ||||||
							
								
								
									
										24
									
								
								src/core/copy-object.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/core/copy-object.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | |||||||
|  | import { Client, CopyDestinationOptions, CopySourceOptions } from 'minio'; | ||||||
|  |  | ||||||
|  | type CopyObjectOpts = { | ||||||
|  |   bucketName: string; | ||||||
|  |   newMetadata: Record<string, string>; | ||||||
|  |   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; | ||||||
|  | }; | ||||||
							
								
								
									
										49
									
								
								src/core/type.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								src/core/type.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -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<any>; | ||||||
|  |   /** | ||||||
|  |    * 上传对象 | ||||||
|  |    * @param objectName 对象名 | ||||||
|  |    * @param data 数据 | ||||||
|  |    */ | ||||||
|  |   putObject(objectName: string, data: Buffer | string, metaData?: ItemBucketMetadata): Promise<UploadedObjectInfo>; | ||||||
|  |   /** | ||||||
|  |    * 上传文件 | ||||||
|  |    * @param objectName 对象名 | ||||||
|  |    * @param filePath 文件路径 | ||||||
|  |    */ | ||||||
|  |   fPutObject(objectName: string, filePath: string, metaData?: ItemBucketMetadata): Promise<UploadedObjectInfo>; | ||||||
|  |   /** | ||||||
|  |    * 获取对象信息 | ||||||
|  |    * @param objectName 对象名 | ||||||
|  |    */ | ||||||
|  |   statObject(objectName: string): Promise<any>; | ||||||
|  |   /** | ||||||
|  |    * 删除对象 | ||||||
|  |    * @param objectName 对象名 | ||||||
|  |    */ | ||||||
|  |   deleteObject(objectName: string): Promise<any>; | ||||||
|  |   /** | ||||||
|  |    * 列出对象 | ||||||
|  |    * @param prefix 前缀 | ||||||
|  |    */ | ||||||
|  |   listObjects(prefix: string, opts?: { recursive?: boolean }): Promise<any>; | ||||||
|  |   /** | ||||||
|  |    * 复制对象 | ||||||
|  |    * @param sourceObject 源对象 | ||||||
|  |    * @param targetObject 目标对象 | ||||||
|  |    */ | ||||||
|  |   copyObject: Client['copyObject']; | ||||||
|  | } | ||||||
							
								
								
									
										59
									
								
								src/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								src/index.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -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; | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										32
									
								
								tsconfig.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								tsconfig.json
									
									
									
									
									
										Normal file
									
								
							| @@ -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", | ||||||
|  |   ], | ||||||
|  | } | ||||||
							
								
								
									
										14
									
								
								tsup.config.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								tsup.config.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -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', | ||||||
|  | }); | ||||||
		Reference in New Issue
	
	Block a user