@be-link/cos 1.12.4 → 1.12.5
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 +23 -70
- package/package.json +1 -1
package/bin/upload.js
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
|
|
4
4
|
const fs = require('fs');
|
|
5
5
|
const path = require('path');
|
|
6
|
-
const https = require('https');
|
|
7
6
|
const COS = require('cos-nodejs-sdk-v5');
|
|
8
7
|
const { getBucketConfig, getRegion } = require('../dist/index.cjs.js');
|
|
9
8
|
|
|
@@ -55,78 +54,32 @@ let cos = null;
|
|
|
55
54
|
/**
|
|
56
55
|
* 通过云函数获取临时密钥(参照浏览器端实现)
|
|
57
56
|
*/
|
|
58
|
-
function getTempCredentials(mode) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
? 'https://shield-60660-10-1304510571.sh.run.tcloudbase.com/config/get-cos-temp-secret'
|
|
64
|
-
: 'https://shield-74680-5-1304510571.sh.run.tcloudbase.com/config/get-cos-temp-secret';
|
|
65
|
-
|
|
66
|
-
const postData = JSON.stringify({});
|
|
67
|
-
|
|
68
|
-
const options = {
|
|
69
|
-
path: url,
|
|
70
|
-
method: 'POST',
|
|
71
|
-
headers: {
|
|
72
|
-
'Content-Type': 'application/json',
|
|
73
|
-
'Content-Length': Buffer.byteLength(postData),
|
|
74
|
-
},
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
console.log('🔐 请求 URL:', url);
|
|
78
|
-
console.log('🔐 请求选项:', options);
|
|
79
|
-
|
|
80
|
-
const req = https.request(options, (res) => {
|
|
81
|
-
let data = '';
|
|
82
|
-
|
|
83
|
-
console.log('🔐 响应状态码:', res.statusCode);
|
|
84
|
-
console.log('🔐 响应头:', res.headers);
|
|
85
|
-
|
|
86
|
-
res.on('data', (chunk) => {
|
|
87
|
-
data += chunk;
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
res.on('end', () => {
|
|
91
|
-
console.log('🔐 原始响应数据:', data);
|
|
92
|
-
|
|
93
|
-
try {
|
|
94
|
-
const result = JSON.parse(data);
|
|
95
|
-
console.log('🔐 解析后的响应:', JSON.stringify(result, null, 2));
|
|
96
|
-
|
|
97
|
-
const credentials = result?.data?.credentials;
|
|
57
|
+
async function getTempCredentials(mode) {
|
|
58
|
+
const url =
|
|
59
|
+
mode === 'production'
|
|
60
|
+
? 'https://shield-60660-10-1304510571.sh.run.tcloudbase.com/config/get-cos-temp-secret'
|
|
61
|
+
: 'https://shield-74680-5-1304510571.sh.run.tcloudbase.com/config/get-cos-temp-secret';
|
|
98
62
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
console.log('✅ 密钥信息获取成功');
|
|
106
|
-
resolve({
|
|
107
|
-
TmpSecretId: credentials.tmpSecretId,
|
|
108
|
-
TmpSecretKey: credentials.tmpSecretKey,
|
|
109
|
-
SecurityToken: credentials.sessionToken,
|
|
110
|
-
StartTime: result.data.startTime,
|
|
111
|
-
ExpiredTime: result.data.expiredTime,
|
|
112
|
-
});
|
|
113
|
-
} catch (err) {
|
|
114
|
-
console.error('❌ JSON 解析失败:', err.message);
|
|
115
|
-
console.error('❌ 原始数据:', data);
|
|
116
|
-
reject(new Error('解析临时密钥响应失败:' + err.message));
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
});
|
|
63
|
+
try {
|
|
64
|
+
const response = await fetch(url, { method: 'POST' });
|
|
65
|
+
const result = await response.json();
|
|
66
|
+
const data = result?.data || {};
|
|
67
|
+
const credentials = data?.credentials;
|
|
120
68
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
});
|
|
69
|
+
if (!credentials) {
|
|
70
|
+
throw new Error('获取临时密钥失败:返回数据格式错误');
|
|
71
|
+
}
|
|
125
72
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
73
|
+
return {
|
|
74
|
+
TmpSecretId: credentials.tmpSecretId,
|
|
75
|
+
TmpSecretKey: credentials.tmpSecretKey,
|
|
76
|
+
SecurityToken: credentials.sessionToken,
|
|
77
|
+
StartTime: data.startTime,
|
|
78
|
+
ExpiredTime: data.expiredTime,
|
|
79
|
+
};
|
|
80
|
+
} catch (error) {
|
|
81
|
+
throw new Error('获取临时密钥失败:' + error.message);
|
|
82
|
+
}
|
|
130
83
|
}
|
|
131
84
|
|
|
132
85
|
/**
|