| | |
| | | */ |
| | | export const uploadToCOS = async (file: File): Promise<string> => { |
| | | try { |
| | | console.log('开始获取上传凭证...') |
| | | |
| | | // 获取上传凭证 |
| | | const credentials = await getUploadCredentials() |
| | | console.log('获取到上传凭证:', credentials) |
| | | |
| | | // 使用预签名URL |
| | | const uploadUrl = credentials.presignedUrl |
| | | |
| | | console.log('开始上传文件到:', uploadUrl) |
| | | |
| | | // 使用预签名URL上传文件 |
| | | const uploadResponse = await axios.put(uploadUrl, file, { |
| | |
| | | }) |
| | | |
| | | if (uploadResponse.status === 200) { |
| | | console.log('文件上传成功!') |
| | | // 返回文件的访问URL(去掉查询参数) |
| | | const fileUrl = `https://${credentials.bucket}.cos.${credentials.region}.myqcloud.com/${credentials.key}` |
| | | return fileUrl |
| | |
| | | } |
| | | |
| | | } catch (error) { |
| | | console.error('上传文件失败:', error) |
| | | throw error |
| | | } |
| | | } |