putString
Upload string content to cloud storage buckets with specified filenames, directories, and content types for data persistence and management.
Instructions
上传字符串内容到存储桶
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | 要上传的字符串内容 | |
| fileName | Yes | 文件名 (存在存储桶里的名称) | |
| targetDir | No | 目标目录 (存在存储桶的哪个目录) | |
| contentType | No | 内容类型,如 text/plain, application/json 等,默认为 text/plain |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"content": {
"description": "要上传的字符串内容",
"type": "string"
},
"contentType": {
"description": "内容类型,如 text/plain, application/json 等,默认为 text/plain",
"type": "string"
},
"fileName": {
"description": "文件名 (存在存储桶里的名称)",
"type": "string"
},
"targetDir": {
"description": "目标目录 (存在存储桶的哪个目录)",
"type": "string"
}
},
"required": [
"content",
"fileName"
],
"type": "object"
}
Implementation Reference
- src/server.ts:114-146 (registration)MCP tool registration for 'putString' including description, Zod input schema, and thin async handler that delegates to CosService.uploadString and formats response.server.tool( 'putString', '上传字符串内容到存储桶', { content: z.string().describe('要上传的字符串内容'), fileName: z.string().describe('文件名 (存在存储桶里的名称)'), targetDir: z .string() .optional() .describe('目标目录 (存在存储桶的哪个目录)'), contentType: z .string() .optional() .describe('内容类型,如 text/plain, application/json 等,默认为 text/plain'), }, async ({ content, fileName, targetDir, contentType }) => { const res = await COSInstance.uploadString({ content, fileName, targetDir, contentType, }); return { content: [ { type: 'text', text: JSON.stringify(res.data, null, 2), }, ], isError: !res.isSuccess, }; }, );
- src/services/cos/cos.service.ts:157-188 (handler)Core implementation of string upload to COS: Zod validation, path construction via buildCosPath, cos.putObject call with Body as string content, and success/error response.async uploadString(params: UploadStringParams) { const validParams = UploadStringParamsSchema.parse(params); const { content, fileName, targetDir = '', contentType = 'text/plain' } = validParams; try { // 构建COS路径 const cosPath = this.buildCosPath(fileName, targetDir); // 上传字符串内容 const cosParams: COS.PutObjectParams = { Bucket: this.bucket, Region: this.region, Key: cosPath, Body: content, ContentType: contentType, }; const result = await this.cos.putObject(cosParams); return { isSuccess: true, message: '上传成功', data: result, }; } catch (error) { return { isSuccess: false, message: '上传失败', data: error, }; } }
- Zod input schema definition for the uploadString method parameters in CosService.export const UploadStringParamsSchema = z.object({ content: z.string(), fileName: z.string(), targetDir: z.string().optional(), contentType: z.string().optional() }); export type UploadStringParams = z.infer<typeof UploadStringParamsSchema>;
- Helper method to construct the COS object key/path from filename and optional target directory.private buildCosPath(fileName: string, targetDir?: string): string { if (!targetDir) { return fileName; } // 规范化目标目录:移除头尾斜杠 const normalizedDir = targetDir.replace(/^\/+|\/+$/g, ''); return normalizedDir ? `${normalizedDir}/${fileName}` : fileName; }