简体中文
几分钟内开始使用文件上传 API,支持多种上传方式
https://kieai.redpandaai.co
Authorization: Bearer YOUR_API_KEY
curl -X POST "https://kieai.redpandaai.co/api/file-url-upload" \ -H "Authorization: Bearer YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "fileUrl": "https://example.com/sample-image.jpg", "uploadPath": "images", "fileName": "my-image.jpg" }'
{ "success": true, "code": 200, "msg": "文件上传成功", "data": { "fileId": "file_abc123456", "fileName": "my-image.jpg", "originalName": "sample-image.jpg", "fileSize": 245760, "mimeType": "image/jpeg", "uploadPath": "images", "fileUrl": "https://kieai.redpandaai.co/files/images/my-image.jpg", "downloadUrl": "https://kieai.redpandaai.co/download/file_abc123456", "uploadTime": "2025-01-15T10:30:00Z", "expiresAt": "2025-01-18T10:30:00Z" } }
class FileUploadAPI { constructor(apiKey) { this.apiKey = apiKey; this.baseUrl = 'https://kieai.redpandaai.co'; } async uploadFile(file, uploadPath = '', fileName = null) { const formData = new FormData(); formData.append('file', file); if (uploadPath) formData.append('uploadPath', uploadPath); if (fileName) formData.append('fileName', fileName); const response = await fetch(`${this.baseUrl}/api/file-stream-upload`, { method: 'POST', headers: { 'Authorization': `Bearer ${this.apiKey}` }, body: formData }); if (!response.ok) { throw new Error(`上传失败: ${response.statusText}`); } return response.json(); } async uploadFromUrl(fileUrl, uploadPath = '', fileName = null) { const response = await fetch(`${this.baseUrl}/api/file-url-upload`, { method: 'POST', headers: { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ fileUrl, uploadPath, fileName }) }); if (!response.ok) { throw new Error(`上传失败: ${response.statusText}`); } return response.json(); } async uploadBase64(base64Data, uploadPath = '', fileName = null) { const response = await fetch(`${this.baseUrl}/api/file-base64-upload`, { method: 'POST', headers: { 'Authorization': `Bearer ${this.apiKey}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ base64Data, uploadPath, fileName }) }); if (!response.ok) { throw new Error(`上传失败: ${response.statusText}`); } return response.json(); } } // 使用示例 const uploader = new FileUploadAPI('YOUR_API_KEY'); // 批量上传文件 async function uploadMultipleFiles(files) { const results = []; for (let i = 0; i < files.length; i++) { try { const result = await uploader.uploadFile( files[i], 'user-uploads', `file-${i + 1}-${files[i].name}` ); results.push(result); console.log(`文件 ${i + 1} 上传成功:`, result.data.fileUrl); } catch (error) { console.error(`文件 ${i + 1} 上传失败:`, error.message); } } return results; } // 从URL批量上传 async function uploadFromUrls(urls) { const results = []; for (let i = 0; i < urls.length; i++) { try { const result = await uploader.uploadFromUrl( urls[i], 'downloads', `download-${i + 1}.jpg` ); results.push(result); console.log(`URL ${i + 1} 上传成功:`, result.data.fileUrl); } catch (error) { console.error(`URL ${i + 1} 上传失败:`, error.message); } } return results; }
401 未授权
// 检查API密钥是否正确 if (response.status === 401) { console.error('API密钥无效,请检查Authorization头'); // 重新获取或更新API密钥 }
400 参数错误
// 检查请求参数 if (response.status === 400) { const error = await response.json(); console.error('请求参数错误:', error.msg); // 检查必填参数是否提供 // 检查文件格式是否支持 // 检查URL是否可访问 }
500 服务器错误
// 实施重试机制 async function uploadWithRetry(uploadFunction, maxRetries = 3) { for (let i = 0; i < maxRetries; i++) { try { return await uploadFunction(); } catch (error) { if (i === maxRetries - 1) throw error; // 指数退避 const delay = Math.pow(2, i) * 1000; await new Promise(resolve => setTimeout(resolve, delay)); } } }
文件大小优化
性能优化
安全考虑
错误处理
expiresAt
downloadUrl