import COS, { COSOptions, GetAuthorizationCallbackParams, PutObjectParams, PutObjectResult, UploadFileParams } from 'cos-nodejs-sdk-v5'; import fs from 'fs'; import path from 'path'; import querystring from 'querystring'; import base64url from 'base64url'; import { DocumentFieldItem, DocumentUploadCosParams } from '../types/vector/document'; import { Logger } from './Logger'; // 定义结果类型 interface UploadResult { uploadCondition: { maxSupportContentLength: number; }; credentials: { TmpSecretId: string; TmpSecretKey: string; Token: string; }; cosRegion: string; cosBucket: string; uploadPath: string; documentSetId: string; } // 定义元数据类型 // interface Metadata { // [key: string]: string | number | string[]; // } export class COSUtils { static async uploadFile(result: DocumentUploadCosParams, localFilePath: string, metadata: DocumentFieldItem, cosConfig?: { append_title_to_chunk?: 0 | 1 append_keywords_to_chunk?: 0 | 1 }): Promise { // 文件Size校验 const limitSize: number = result.uploadCondition.maxSupportContentLength; const fileStat = fs.statSync(localFilePath); if (limitSize < fileStat.size) { // throw new Error('The file exceeds its maximum permitted size'); return Promise.reject(new Error('The file exceeds its maximum permitted size')); } // 设置COS上传签名 // const credentials = result.credentials; // const options: COSOptions = { // // Credentials: { // // secretId: result.credentials.TmpSecretId, // // secretKey: result.credentials.TmpSecretKey, // // }, // SecretId: result.credentials.TmpSecretId, // SecretKey: result.credentials.TmpSecretKey, // SecurityToken: result.credentials.Token, // } // Logger.debug('COS upload options', options) const now = parseInt((Date.now() / 1000).toString()) const cos = new COS({ getAuthorization: (options, callbalk) => { callbalk({ TmpSecretId: result.credentials.TmpSecretId, TmpSecretKey: result.credentials.TmpSecretKey, SecurityToken: result.credentials.Token, StartTime: parseInt((Date.now() / 1000).toString()), // 时间戳,单位秒,如:1580000000 ExpiredTime: now + 600, // 临时密钥失效时间戳,是申请临时密钥时,时间戳加 durationSeconds ScopeLimit: true, // 细粒度控制权限需要设为 true,会限制密钥只在相同请求时重复使用 } as GetAuthorizationCallbackParams) } }); // 自定义文件 Metadata 信息 // const metadata: Metadata = { // fieldStr: 'v1', // fieldInt: 1024, // fieldList: ['a', 'b', 'c'], // }; metadata = metadata || {} // 配置预处理逻辑 const config = { appendTitleToChunk: cosConfig?.append_title_to_chunk || 1, appendKeywordsToChunk: cosConfig?.append_keywords_to_chunk || 1, }; const cosMetadata = { 'x-cos-meta-id': result.documentSetId, // 文件唯一标识符 'x-cos-meta-config': Buffer.from(JSON.stringify(config), "utf-8").toString("base64"), 'x-cos-meta-data': Buffer.from(JSON.stringify(metadata), "utf-8").toString("base64"), }; let putParams: UploadFileParams = { Bucket: result.cosBucket, Region: result.cosRegion, Key: result.uploadPath, FilePath: localFilePath } putParams = Object.assign(putParams, cosMetadata); Logger.debug('COS upload params', putParams) // 上传文件 try { const result = await cos.uploadFile(putParams) return result } catch (err) { return Promise.reject(err) } } }