putObjectSourceUrl
Downloads files from a URL and uploads them to cloud storage buckets for automated file transfer and management.
Instructions
通过 url下载文件并将文件上传到存储桶
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sourceUrl | Yes | 可下载的文件 url | |
| fileName | No | 文件名 (存在存储桶里的名称) | |
| targetDir | No | 目标目录 (存在存储桶的哪个目录) |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"fileName": {
"description": "文件名 (存在存储桶里的名称)",
"type": "string"
},
"sourceUrl": {
"description": "可下载的文件 url",
"type": "string"
},
"targetDir": {
"description": "目标目录 (存在存储桶的哪个目录)",
"type": "string"
}
},
"required": [
"sourceUrl"
],
"type": "object"
}
Implementation Reference
- src/services/cos/cos.service.ts:281-313 (handler)Core handler implementation: downloads file from sourceUrl using axios stream, generates filename if needed, builds COS path, and uploads via cos.putObject using PassThrough stream.async uploadFileSourceUrl(params: UploadFileParams) { // 验证并解析参数 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(''); const cosPath = this.buildCosPath(actualFileName, targetDir); 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, }; } }
- src/server.ts:221-248 (registration)MCP tool registration for 'putObjectSourceUrl', defines input schema with Zod and inline handler that delegates to CosService.uploadFileSourceUrlserver.tool( 'putObjectSourceUrl', '通过 url下载文件并将文件上传到存储桶', { sourceUrl: z.string().describe('可下载的文件 url'), fileName: z.string().optional().describe('文件名 (存在存储桶里的名称)'), targetDir: z .string() .optional() .describe('目标目录 (存在存储桶的哪个目录)'), }, async ({ sourceUrl, fileName, targetDir}) => { const res = await COSInstance.uploadFileSourceUrl({ targetDir, fileName, sourceUrl, }); return { content: [ { type: 'text', text: JSON.stringify(res.data, null, 2), }, ], isError: !res.isSuccess, }; }, );
- Zod schema used in the handler for validating upload parameters including sourceUrl.export const UploadFileParamsSchema = z.object({ filePath: z.string().optional(), targetDir: z.string().optional(), fileName: z.string().optional(), sourceUrl: z.string().optional() }); export type UploadFileParams = z.infer<typeof UploadFileParamsSchema>;