Skip to content

Commit

Permalink
Feat/2.11.17 beta (#158)
Browse files Browse the repository at this point in the history
* feat: 支持动态秘钥Cred

* upd version

* fix: 修改字段
  • Loading branch information
livehigh authored Dec 6, 2022
1 parent 860d980 commit f214503
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
11 changes: 8 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ declare namespace COS {
params: GetAuthorizationCallbackParams
) => void
) => void,
/** 支持动态Credentials */
Credentials?: {
secretId?: string;
secretKey?: string;
};
}

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

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

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

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

/** 判断上传队列是否有未完成的任务 */
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cos-nodejs-sdk-v5",
"version": "2.11.16",
"version": "2.11.17",
"description": "cos nodejs sdk v5",
"main": "index.js",
"types": "index.d.ts",
Expand Down
33 changes: 33 additions & 0 deletions sdk/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ var defaultOptions = {
UserAgent: '',
ConfCwd: '',
ForceSignHost: true, // 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true
// 动态秘钥,优先级Credentials > SecretId/SecretKey。注意Cred内是小写的secretId、secretKey
Credentials: {
secretId: '',
secretKey: '',
},
};

const watch = (obj, name, callback) => {
let value = obj[name];
Object.defineProperty(obj, name, {
get() {
return value;
},
set(newValue) {
value = newValue;
callback();
}
});
};

// 对外暴露的类
Expand All @@ -66,6 +84,11 @@ var COS = function (options) {
if (this.options.secretId && !this.options.SecretId) this.options.SecretId = this.options.secretId;
if (this.options.secretKey && !this.options.SecretKey) this.options.SecretKey = this.options.secretKey;
console.warn('warning: Please change options secretId/secretKey to SecretId/SecretKey.');
}
// 支持外部传入Cred动态秘钥
if (this.options.Credentials) {
this.options.SecretId = this.options.Credentials.secretId || '';
this.options.SecretKey = this.options.Credentials.secretKey || '';
}
if (this.options.SecretId && this.options.SecretId.indexOf(' ') > -1) {
console.error('error: SecretId格式错误,请检查');
Expand All @@ -81,6 +104,16 @@ var COS = function (options) {
}
event.init(this);
task.init(this);

// 支持动态秘钥,监听到cred里secretId、secretKey变化时,主动给cos替换秘钥
watch(this.options.Credentials, 'secretId', () => {
console.log('Credentials secretId changed');
this.options.SecretId = this.options.Credentials.secretId;
});
watch(this.options.Credentials, 'secretKey', () => {
console.log('Credentials secretKey changed');
this.options.SecretKey = this.options.Credentials.secretKey;
});
};

base.init(COS, task);
Expand Down

0 comments on commit f214503

Please sign in to comment.