upload_file
Upload a single file from your local system to Tencent Cloud Object Storage (COS) for secure cloud storage management.
Instructions
上传单个文件到腾讯云COS
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| custom_domain | No | 自定义访问域名(可选) | |
| file_path | Yes | 本地文件路径 | |
| object_key | No | 上传后在COS中的对象键名,如果未提供则使用文件名 |
Implementation Reference
- index.js:412-425 (handler)MCP tool handler for 'upload_file': validates local file path existence and delegates upload to cosService.uploadFile, returning formatted success response.case 'upload_file': const cleanPath = validateFileExists(args.file_path); const result = await cosService.uploadFile(cleanPath, { key: args.object_key, customDomain: args.custom_domain }); return { content: [ { type: 'text', text: JSON.stringify({ success: true, data: result }, null, 2) } ] };
- index.js:47-68 (schema)Schema definition for 'upload_file' tool, including input parameters: file_path (required), object_key, custom_domain.upload_file: { name: 'upload_file', description: '上传单个文件到腾讯云COS', inputSchema: { type: 'object', properties: { file_path: { type: 'string', description: '本地文件路径' }, object_key: { type: 'string', description: '上传后在COS中的对象键名,如果未提供则使用文件名' }, custom_domain: { type: 'string', description: '自定义访问域名(可选)' } }, required: ['file_path'] } },
- src/cosService.js:53-126 (helper)Core uploadFile method in cosService: handles both small file direct upload and large file slice upload (delegates to _uploadLargeFile if >5MB), manages temp files, returns upload result with URL and metadata.async uploadFile(localPath, { key = null, customDomain = null, useSliceUpload = false, chunkSize = 1024 * 1024, // 1MB concurrency = 3, onProgress = null } = {}) { this._checkConfig(); try { // 获取文件状态 const stats = await fs.stat(localPath); const fileSize = stats.size; // 生成key,如果没提供则使用文件名 const objectKey = key || path.basename(localPath); // 判断是否使用分片上传 (文件大于5MB或强制指定) const shouldUseSliceUpload = useSliceUpload || fileSize > (5 * 1024 * 1024); if (shouldUseSliceUpload) { return await this._uploadLargeFile(localPath, objectKey, { customDomain, chunkSize, concurrency, onProgress, fileSize }); } // 小文件直接上传 const buffer = await fs.readFile(localPath); const params = { Bucket: this.config.Bucket, Region: this.config.Region, Key: objectKey, Body: buffer, }; const response = await this.cos.putObject(params); // 上传成功后清理相关临时文件 try { await this._cleanupUploadTempFiles(objectKey); } catch (clearErr) { // 清理临时文件失败时静默处理,不影响上传成功的结果 } // 生成访问URL const domain = customDomain || this.config.Domain || `https://${this.config.Bucket}.cos.${this.config.Region}.myqcloud.com`; const fileUrl = `${domain}/${objectKey}`; return { success: true, url: fileUrl, key: objectKey, etag: response.ETag, location: response.Location, size: fileSize, uploadType: 'direct' }; } catch (error) { // 上传失败时,清理可能产生的临时文件 try { await this._cleanupUploadTempFiles(objectKey); } catch (clearErr) { // 清理失败时静默处理 } console.error('上传失败:', error); throw new Error(`文件上传失败: ${error.message}`); } }