lrj
2 天以前 c61d4fe27c97d2ecc907756aa571a4ef14a7b9b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// 直接测试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()');