This commit is contained in:
xion 2025-03-22 19:47:23 +08:00
commit 5563ded0a1
9 changed files with 213 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules
dist

2
.npmrc Normal file
View File

@ -0,0 +1,2 @@
//npm.xiongxiao.me/:_authToken=${ME_NPM_TOKEN}
//registry.npmjs.org/:_authToken=${NPM_TOKEN}

22
package.json Normal file
View 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
View File

@ -0,0 +1,9 @@
# oss 管理对minio的封装
一般常用的功能模块。
- 上传文件
- 下载文件
- 删除文件
- 复制文件
- 列出文件

24
src/core/copy-object.ts Normal file
View 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
View 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
View 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
View 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
View 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',
});