// 直接测试COS上传功能
|
async function testDirectUpload() {
|
try {
|
console.log('=== 开始直接上传测试 ===');
|
|
// 1. 获取上传凭证
|
console.log('1. 获取上传凭证...');
|
const response = await fetch('http://localhost:8080/api/graphql', {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query: `
|
query GetUploadCredentials {
|
getUploadCredentials {
|
bucket
|
region
|
key
|
presignedUrl
|
expiration
|
}
|
}
|
`
|
})
|
});
|
|
const result = await response.json();
|
console.log('GraphQL响应:', result);
|
|
if (result.errors) {
|
throw new Error('GraphQL错误: ' + result.errors[0].message);
|
}
|
|
const credentials = result.data.getUploadCredentials;
|
console.log('获取到的凭证:', credentials);
|
|
// 2. 获取logo文件
|
console.log('2. 获取logo文件...');
|
const logoResponse = await fetch('/UI/logo.jpg');
|
if (!logoResponse.ok) {
|
throw new Error('无法获取logo文件: ' + logoResponse.status);
|
}
|
|
const logoBlob = await logoResponse.blob();
|
console.log('Logo文件信息:', {
|
size: logoBlob.size,
|
type: logoBlob.type
|
});
|
|
// 3. 上传到COS
|
console.log('3. 上传到COS...');
|
console.log('上传URL:', credentials.presignedUrl);
|
|
const uploadResponse = await fetch(credentials.presignedUrl, {
|
method: 'PUT',
|
body: logoBlob,
|
headers: {
|
'Content-Type': logoBlob.type || 'image/jpeg',
|
}
|
});
|
|
console.log('上传响应状态:', uploadResponse.status);
|
console.log('上传响应头:', Object.fromEntries(uploadResponse.headers.entries()));
|
|
if (uploadResponse.ok) {
|
const fileUrl = `https://${credentials.bucket}.cos.${credentials.region}.myqcloud.com/${credentials.key}`;
|
console.log('✅ 上传成功!');
|
console.log('文件访问URL:', fileUrl);
|
return fileUrl;
|
} else {
|
const errorText = await uploadResponse.text();
|
console.error('❌ 上传失败:', uploadResponse.status, errorText);
|
throw new Error(`上传失败: ${uploadResponse.status} - ${errorText}`);
|
}
|
|
} catch (error) {
|
console.error('❌ 测试失败:', error);
|
throw error;
|
}
|
}
|
|
// 在浏览器控制台中运行: testDirectUpload()
|
console.log('直接上传测试函数已加载,请在控制台运行: testDirectUpload()');
|