@be-link/cos 1.12.2 → 1.12.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/upload.js +24 -4
- package/package.json +1 -1
package/bin/upload.js
CHANGED
|
@@ -57,38 +57,52 @@ let cos = null;
|
|
|
57
57
|
*/
|
|
58
58
|
function getTempCredentials(mode) {
|
|
59
59
|
return new Promise((resolve, reject) => {
|
|
60
|
+
console.log('🔐 环境模式:', mode);
|
|
60
61
|
const url =
|
|
61
62
|
mode === 'production'
|
|
62
63
|
? 'https://shield-60660-10-1304510571.sh.run.tcloudbase.com/config/get-cos-temp-secret'
|
|
63
64
|
: 'https://shield-74680-5-1304510571.sh.run.tcloudbase.com/config/get-cos-temp-secret';
|
|
64
65
|
|
|
65
|
-
const
|
|
66
|
+
const postData = JSON.stringify({});
|
|
67
|
+
|
|
66
68
|
const options = {
|
|
67
|
-
|
|
68
|
-
path: urlObj.pathname,
|
|
69
|
+
path: url,
|
|
69
70
|
method: 'POST',
|
|
70
71
|
headers: {
|
|
71
72
|
'Content-Type': 'application/json',
|
|
73
|
+
'Content-Length': Buffer.byteLength(postData),
|
|
72
74
|
},
|
|
73
75
|
};
|
|
74
76
|
|
|
77
|
+
console.log('🔐 请求 URL:', url);
|
|
78
|
+
console.log('🔐 请求选项:', options);
|
|
79
|
+
|
|
75
80
|
const req = https.request(options, (res) => {
|
|
76
81
|
let data = '';
|
|
77
82
|
|
|
83
|
+
console.log('🔐 响应状态码:', res.statusCode);
|
|
84
|
+
console.log('🔐 响应头:', res.headers);
|
|
85
|
+
|
|
78
86
|
res.on('data', (chunk) => {
|
|
79
87
|
data += chunk;
|
|
80
88
|
});
|
|
81
89
|
|
|
82
90
|
res.on('end', () => {
|
|
91
|
+
console.log('🔐 原始响应数据:', data);
|
|
92
|
+
|
|
83
93
|
try {
|
|
84
94
|
const result = JSON.parse(data);
|
|
95
|
+
console.log('🔐 解析后的响应:', JSON.stringify(result, null, 2));
|
|
96
|
+
|
|
85
97
|
const credentials = result?.data?.credentials;
|
|
86
98
|
|
|
87
99
|
if (!credentials) {
|
|
100
|
+
console.error('❌ 返回数据结构:', result);
|
|
88
101
|
reject(new Error('获取临时密钥失败:返回数据格式错误'));
|
|
89
102
|
return;
|
|
90
103
|
}
|
|
91
104
|
|
|
105
|
+
console.log('✅ 密钥信息获取成功');
|
|
92
106
|
resolve({
|
|
93
107
|
TmpSecretId: credentials.tmpSecretId,
|
|
94
108
|
TmpSecretKey: credentials.tmpSecretKey,
|
|
@@ -97,15 +111,20 @@ function getTempCredentials(mode) {
|
|
|
97
111
|
ExpiredTime: result.data.expiredTime,
|
|
98
112
|
});
|
|
99
113
|
} catch (err) {
|
|
114
|
+
console.error('❌ JSON 解析失败:', err.message);
|
|
115
|
+
console.error('❌ 原始数据:', data);
|
|
100
116
|
reject(new Error('解析临时密钥响应失败:' + err.message));
|
|
101
117
|
}
|
|
102
118
|
});
|
|
103
119
|
});
|
|
104
120
|
|
|
105
121
|
req.on('error', (err) => {
|
|
122
|
+
console.error('❌ 请求错误:', err);
|
|
106
123
|
reject(new Error('请求临时密钥失败:' + err.message));
|
|
107
124
|
});
|
|
108
125
|
|
|
126
|
+
// 发送请求体
|
|
127
|
+
req.write(postData);
|
|
109
128
|
req.end();
|
|
110
129
|
});
|
|
111
130
|
}
|
|
@@ -123,7 +142,8 @@ async function initCOS() {
|
|
|
123
142
|
// 方式1:尝试通过云函数获取临时密钥(推荐)
|
|
124
143
|
console.log('🔐 正在获取临时密钥...');
|
|
125
144
|
const credentials = await getTempCredentials(mode || 'development');
|
|
126
|
-
console.log('✅
|
|
145
|
+
console.log('✅ 临时密钥获取成功');
|
|
146
|
+
console.log(' 有效期至:', new Date(credentials.ExpiredTime * 1000).toLocaleString());
|
|
127
147
|
|
|
128
148
|
cos = new COS({
|
|
129
149
|
getAuthorization: (options, callback) => {
|