putBuffer
Upload buffer content to cloud storage buckets with configurable encoding and content types for file management.
Instructions
上传buffer内容到存储桶
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | buffer内容字符串 | |
| fileName | Yes | 文件名 (存在存储桶里的名称) | |
| targetDir | No | 目标目录 (存在存储桶的哪个目录) | |
| contentType | No | 内容类型,如 image/png, application/pdf 等,默认为 application/octet-stream | |
| encoding | No | 字符串编码格式,默认为utf8。hex=十六进制,base64=Base64编码,utf8=UTF-8文本,ascii=ASCII文本,binary=二进制 |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"content": {
"description": "buffer内容字符串",
"type": "string"
},
"contentType": {
"description": "内容类型,如 image/png, application/pdf 等,默认为 application/octet-stream",
"type": "string"
},
"encoding": {
"description": "字符串编码格式,默认为utf8。hex=十六进制,base64=Base64编码,utf8=UTF-8文本,ascii=ASCII文本,binary=二进制",
"enum": [
"hex",
"base64",
"utf8",
"ascii",
"binary"
],
"type": "string"
},
"fileName": {
"description": "文件名 (存在存储桶里的名称)",
"type": "string"
},
"targetDir": {
"description": "目标目录 (存在存储桶的哪个目录)",
"type": "string"
}
},
"required": [
"content",
"fileName"
],
"type": "object"
}
Implementation Reference
- src/server.ts:182-219 (registration)Registration of the 'putBuffer' MCP tool, including inline input schema, description, and handler function that calls CosService.uploadBuffer and returns formatted response.server.tool( 'putBuffer', '上传buffer内容到存储桶', { content: z.string().describe('buffer内容字符串'), fileName: z.string().describe('文件名 (存在存储桶里的名称)'), targetDir: z .string() .optional() .describe('目标目录 (存在存储桶的哪个目录)'), contentType: z .string() .optional() .describe('内容类型,如 image/png, application/pdf 等,默认为 application/octet-stream'), encoding: z .enum(['hex', 'base64', 'utf8', 'ascii', 'binary']) .optional() .describe('字符串编码格式,默认为utf8。hex=十六进制,base64=Base64编码,utf8=UTF-8文本,ascii=ASCII文本,binary=二进制'), }, async ({ content, fileName, targetDir, contentType, encoding }) => { const res = await COSInstance.uploadBuffer({ content, fileName, targetDir, contentType, encoding, }); return { content: [ { type: 'text', text: JSON.stringify(res.data, null, 2), }, ], isError: !res.isSuccess, }; }, );
- Zod schema for the uploadBuffer method parameters, matching the tool's input schema.export const UploadBufferParamsSchema = z.object({ content: z.string(), fileName: z.string(), targetDir: z.string().optional(), contentType: z.string().optional(), encoding: z.enum(['hex', 'base64', 'utf8', 'ascii', 'binary']).optional() }); export type UploadBufferParams = z.infer<typeof UploadBufferParamsSchema>;
- Implementation of uploadBuffer in CosService: validates params, builds object key, converts content string to Buffer (with optional encoding), uploads to COS via putObject, handles errors.async uploadBuffer(params: UploadBufferParams) { const validParams = UploadBufferParamsSchema.parse(params); const { content, fileName, targetDir = '', contentType = 'application/octet-stream', encoding } = validParams; try { // 构建COS路径 const cosPath = this.buildCosPath(fileName, targetDir); // 根据编码类型转换为Buffer const buffer = encoding ? Buffer.from(content, encoding) : Buffer.from(content); // 上传buffer内容 const cosParams: COS.PutObjectParams = { Bucket: this.bucket, Region: this.region, Key: cosPath, Body: buffer, ContentType: contentType, }; const result = await this.cos.putObject(cosParams); return { isSuccess: true, message: '上传成功', data: result, }; } catch (error) { return { isSuccess: false, message: '上传失败', data: error, }; } }