Skip to content

Commit

Permalink
feat: add refreshSTSToken example (#924)
Browse files Browse the repository at this point in the history
  • Loading branch information
beajer authored Feb 18, 2021
1 parent 5aec639 commit f69d8ed
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
38 changes: 22 additions & 16 deletions lib/common/client/initOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f69d8ed

Please sign in to comment.