@be-link/cos 1.12.4 → 1.12.6

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.
Files changed (2) hide show
  1. package/bin/upload.js +28 -73
  2. 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,34 @@ let cos = null;
55
54
  /**
56
55
  * 通过云函数获取临时密钥(参照浏览器端实现)
57
56
  */
58
- function getTempCredentials(mode) {
59
- return new Promise((resolve, reject) => {
60
- console.log('🔐 环境模式:', mode);
61
- const url =
62
- mode === 'production'
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);
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';
85
62
 
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;
98
-
99
- if (!credentials) {
100
- console.error('❌ 返回数据结构:', result);
101
- reject(new Error('获取临时密钥失败:返回数据格式错误'));
102
- return;
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
- });
120
-
121
- req.on('error', (err) => {
122
- console.error('❌ 请求错误:', err);
123
- reject(new Error('请求临时密钥失败:' + err.message));
124
- });
63
+ try {
64
+ const response = await fetch(url, { method: 'POST' });
65
+ console.log('🔐 响应:', response);
66
+ const result = await response.json();
67
+ console.log('🔐 结果:', result);
68
+ const data = result?.data || {};
69
+ const credentials = data?.credentials;
70
+
71
+ if (!credentials) {
72
+ throw new Error('获取临时密钥失败:返回数据格式错误');
73
+ }
125
74
 
126
- // 发送请求体
127
- req.write(postData);
128
- req.end();
129
- });
75
+ return {
76
+ TmpSecretId: credentials.tmpSecretId,
77
+ TmpSecretKey: credentials.tmpSecretKey,
78
+ SecurityToken: credentials.sessionToken,
79
+ StartTime: data.startTime,
80
+ ExpiredTime: data.expiredTime,
81
+ };
82
+ } catch (error) {
83
+ throw new Error('获取临时密钥失败:' + error.message);
84
+ }
130
85
  }
131
86
 
132
87
  /**
@@ -176,8 +131,8 @@ async function initCOS() {
176
131
 
177
132
  console.log('✅ 使用环境变量中的密钥');
178
133
  cos = new COS({
179
- SecretId: secretId,
180
- SecretKey: secretKey,
134
+ SecretId: 'AKIDx3wfJo5e4FAqHSOJ5cu2y3zh9MK2Uhy6',
135
+ SecretKey: '3sgW7V7W1PZWIqAKI8krpY3JL9j5C3e1',
181
136
  });
182
137
 
183
138
  return cos;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@be-link/cos",
3
- "version": "1.12.4",
3
+ "version": "1.12.6",
4
4
  "description": "前端项目产物上传cos",
5
5
  "homepage": "https://github.com/snowmountain-top/be-link#readme",
6
6
  "author": "zhuiyi",