From f69d8ed8dcebdefaee5acf540d1baef9866ffb26 Mon Sep 17 00:00:00 2001 From: beajer <919060679@qq.com> Date: Thu, 18 Feb 2021 03:07:02 -0600 Subject: [PATCH] feat: add refreshSTSToken example (#924) --- README.md | 18 +++++++++++++++ lib/common/client/initOptions.js | 38 ++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 06cf5fa24..ef8f21561 100644 --- a/README.md +++ b/README.md @@ -378,6 +378,24 @@ const store = new OSS({ }); ``` +4. use STS and refreshSTSToken +```js +const OSS = require('ali-oss'); + +const store = new OSS({ + accessKeyId: 'your STS key', + accessKeySecret: 'your STS secret', + stsToken: 'your STS token', + refreshSTSToken: async () => { + return { + accessKeyId: 'your refreshed STS key', + accessKeySecret: 'your refreshed STS secret', + stsToken: 'your refreshed STS stoken' + } + } +}); +``` + ## Bucket Operations ### .listBuckets(query[, options]) diff --git a/lib/common/client/initOptions.js b/lib/common/client/initOptions.js index e8b2b9ca2..f41a5e4c3 100644 --- a/lib/common/client/initOptions.js +++ b/lib/common/client/initOptions.js @@ -20,27 +20,33 @@ function setEndpoint(endpoint, secure) { } module.exports = function (options) { - if (!options - || !options.accessKeyId - || !options.accessKeySecret) { + if (!options || !options.accessKeyId || !options.accessKeySecret) { throw new Error('require accessKeyId, accessKeySecret'); } + if (options.stsToken && !options.refreshSTSToken) { + console.warn( + "It's recommended to set `refreshSTSToken` to refresh stsToken、accessKeyId、accessKeySecret automatically when sts info expires" + ); + } if (options.bucket) { _checkBucketName(options.bucket); } - const opts = Object.assign({ - region: 'oss-cn-hangzhou', - internal: false, - secure: false, - timeout: 60000, - bucket: null, - endpoint: null, - cname: false, - isRequestPay: false, - sldEnable: false, - headerEncoding: 'utf-8', - refreshSTSToken: null - }, options); + const opts = Object.assign( + { + region: 'oss-cn-hangzhou', + internal: false, + secure: false, + timeout: 60000, + bucket: null, + endpoint: null, + cname: false, + isRequestPay: false, + sldEnable: false, + headerEncoding: 'utf-8', + refreshSTSToken: null + }, + options + ); opts.accessKeyId = opts.accessKeyId.trim(); opts.accessKeySecret = opts.accessKeySecret.trim();