Skip to content

Commit f214503

Browse files
authored
Feat/2.11.17 beta (#158)
* feat: 支持动态秘钥Cred * upd version * fix: 修改字段
1 parent 860d980 commit f214503

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

index.d.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ declare namespace COS {
194194
params: GetAuthorizationCallbackParams
195195
) => void
196196
) => void,
197+
/** 支持动态Credentials */
198+
Credentials?: {
199+
secretId?: string;
200+
secretKey?: string;
201+
};
197202
}
198203

199204
type StringOrBuffer = Buffer | String;
@@ -2294,13 +2299,13 @@ declare class COS {
22942299
/** 获取上传任务列表 */
22952300
getTaskList(): COS.TaskList;
22962301

2297-
/** 判断上传队列是否有未完成的任务 */
2302+
/** 暂停任务 */
22982303
pauseTask(taskId: COS.TaskId): void;
22992304

2300-
/** 判断上传队列是否有未完成的任务 */
2305+
/** 重启任务 */
23012306
restartTask(taskId: COS.TaskId): void;
23022307

2303-
/** 判断上传队列是否有未完成的任务 */
2308+
/** 取消任务 */
23042309
cancelTask(taskId: COS.TaskId): void;
23052310

23062311
/** 判断上传队列是否有未完成的任务 */

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cos-nodejs-sdk-v5",
3-
"version": "2.11.16",
3+
"version": "2.11.17",
44
"description": "cos nodejs sdk v5",
55
"main": "index.js",
66
"types": "index.d.ts",

sdk/cos.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ var defaultOptions = {
4444
UserAgent: '',
4545
ConfCwd: '',
4646
ForceSignHost: true, // 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true
47+
// 动态秘钥,优先级Credentials > SecretId/SecretKey。注意Cred内是小写的secretId、secretKey
48+
Credentials: {
49+
secretId: '',
50+
secretKey: '',
51+
},
52+
};
53+
54+
const watch = (obj, name, callback) => {
55+
let value = obj[name];
56+
Object.defineProperty(obj, name, {
57+
get() {
58+
return value;
59+
},
60+
set(newValue) {
61+
value = newValue;
62+
callback();
63+
}
64+
});
4765
};
4866

4967
// 对外暴露的类
@@ -66,6 +84,11 @@ var COS = function (options) {
6684
if (this.options.secretId && !this.options.SecretId) this.options.SecretId = this.options.secretId;
6785
if (this.options.secretKey && !this.options.SecretKey) this.options.SecretKey = this.options.secretKey;
6886
console.warn('warning: Please change options secretId/secretKey to SecretId/SecretKey.');
87+
}
88+
// 支持外部传入Cred动态秘钥
89+
if (this.options.Credentials) {
90+
this.options.SecretId = this.options.Credentials.secretId || '';
91+
this.options.SecretKey = this.options.Credentials.secretKey || '';
6992
}
7093
if (this.options.SecretId && this.options.SecretId.indexOf(' ') > -1) {
7194
console.error('error: SecretId格式错误,请检查');
@@ -81,6 +104,16 @@ var COS = function (options) {
81104
}
82105
event.init(this);
83106
task.init(this);
107+
108+
// 支持动态秘钥,监听到cred里secretId、secretKey变化时,主动给cos替换秘钥
109+
watch(this.options.Credentials, 'secretId', () => {
110+
console.log('Credentials secretId changed');
111+
this.options.SecretId = this.options.Credentials.secretId;
112+
});
113+
watch(this.options.Credentials, 'secretKey', () => {
114+
console.log('Credentials secretKey changed');
115+
this.options.SecretKey = this.options.Credentials.secretKey;
116+
});
84117
};
85118

86119
base.init(COS, task);

0 commit comments

Comments
 (0)