UNPKG

cos-mcp

Version:

基于MCP协议的腾讯云COS MCP Server,无需编码即可让大模型快速接入腾讯云存储(COS)和数据万象(CI)能力

191 lines 6.12 kB
import fs from 'fs'; import path from 'path'; import { z } from 'zod'; import axios from 'axios'; import { PassThrough } from 'stream'; import { generateOutPutFileId } from '../utils.js'; export const UploadFileParamsSchema = z.object({ filePath: z.string().optional(), targetDir: z.string().optional(), fileName: z.string().optional(), sourceUrl: z.string().optional() }); export class CosService { bucket; region; cos; constructor(bucket, region, cos) { this.bucket = bucket; this.region = region; this.cos = cos; } // /** // * 上传文件到COS // * @param params 上传参数 // * @returns 上传结果 // */ async uploadFile(params) { // 验证并解析参数 const validParams = UploadFileParamsSchema.parse(params); const { filePath, targetDir = '', fileName } = validParams; try { // 检查文件是否存在 if (!filePath || !fs.existsSync(filePath)) { return { isSuccess: false, message: '此路径上文件不存在', data: '此路径上文件不存在: ' + filePath, }; } // 确定文件名 const actualFileName = fileName || path.basename(filePath); // 构建COS路径,确保正斜杠格式 let cosPath = actualFileName; if (targetDir) { // 规范化目标目录:移除头尾斜杠,然后加上结尾斜杠 const normalizedDir = targetDir.replace(/^\/+|\/+$/g, ''); cosPath = normalizedDir ? `${normalizedDir}/${actualFileName}` : actualFileName; } // 上传文件 const cosParams = { Bucket: this.bucket, Region: this.region, Key: cosPath, FilePath: filePath, }; const result = await this.cos.uploadFile(cosParams); return { isSuccess: true, message: '上传成功', data: result, }; } catch (error) { return { isSuccess: false, message: '上传失败', data: error, }; } } // /** // * 上传文件到COS // * @param params 上传参数 // * @returns 上传结果 // */ async uploadFileSourceUrl(params) { // 验证并解析参数 const validParams = UploadFileParamsSchema.parse(params); const { targetDir = '', fileName, sourceUrl } = validParams; try { const response = await axios({ method: 'get', url: sourceUrl, responseType: 'stream' }); const actualFileName = fileName ? fileName : generateOutPutFileId(''); let cosPath = actualFileName; if (targetDir) { // 规范化目标目录:移除头尾斜杠,然后加上结尾斜杠 const normalizedDir = targetDir.replace(/^\/+|\/+$/g, ''); cosPath = normalizedDir ? `${normalizedDir}/${actualFileName}` : actualFileName; } const req = response.data; const passThrough = new PassThrough(); const result = await this.cos.putObject({ Bucket: this.bucket, Region: this.region, Key: cosPath, Body: req.pipe(passThrough), }); return { isSuccess: true, message: '上传成功', data: result, }; } catch (error) { return { isSuccess: false, message: '上传失败', data: error, }; } } async getObject(objectKey = '/') { try { // 下载文件 const cosParams = { Bucket: this.bucket, Region: this.region, Key: objectKey, }; const result = await this.cos.getObject(cosParams); const content = String(result.Body); return { isSuccess: true, message: '下载文件成功', data: content, }; } catch (error) { return { isSuccess: false, message: '下载文件失败', data: error, }; } } // 获取文件列表 async getBucket(Prefix = '') { try { const result = await this.cos.getBucket({ Bucket: this.bucket, Region: this.region, Prefix, Delimiter: '', }); return { isSuccess: true, message: '获取列表成功', data: result, }; } catch (error) { return { isSuccess: false, message: '获取列表失败', data: error, }; } } //获取带签名的objecturl async getObjectUrl(ObjectKey) { try { const result = await new Promise((resolve, reject) => { this.cos.getObjectUrl({ Bucket: this.bucket, Region: this.region, Key: ObjectKey, }, (error, data) => (error ? reject(error) : resolve(data))); }); return { isSuccess: true, message: '获取带签名 ObjectUrl 成功', data: result, }; } catch (error) { return { isSuccess: false, message: '获取带签名 ObjectUrl 失败', data: error, }; } } } //# sourceMappingURL=cos.service.js.map