Skip to content

Commit b737f86

Browse files
committed
支持 STS Scope
1 parent b77ee1f commit b737f86

File tree

7 files changed

+339
-128
lines changed

7 files changed

+339
-128
lines changed

demo/demo-sts-scope.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* 使用临时密钥例子
3+
*/
4+
var STS = require('qcloud-cos-sts');
5+
var COS = require('../index');
6+
var config = require('./config');
7+
8+
var allowPrefix = '';
9+
// 简单上传和分片,需要以下的权限,其他权限列表请看 https://cloud.tencent.com/document/product/436/14048
10+
var allowActions = [
11+
'name/cos:PutObject',
12+
'name/cos:InitiateMultipartUpload',
13+
'name/cos:ListMultipartUploads',
14+
'name/cos:ListParts',
15+
'name/cos:UploadPart',
16+
'name/cos:CompleteMultipartUpload'
17+
];
18+
19+
20+
// 判断是否允许获取密钥
21+
var allowScope = function (scope) {
22+
var allow = (scope || []).every(function (item) {
23+
return allowActions.includes(item.action) &&
24+
item.bucket === config.bucket &&
25+
item.region === config.region &&
26+
(item.prefix || '').startsWith(allowPrefix);
27+
});
28+
return allow;
29+
};
30+
31+
var cos = new COS({
32+
getAuthorization: function (options, callback) {
33+
34+
if (!allowScope()) {
35+
console.log('deny Scope');
36+
return;
37+
}
38+
39+
// 获取临时密钥
40+
var policy = STS.getPolicy(options.Scope);
41+
STS.getCredential({
42+
secretId: config.SecretId,
43+
secretKey: config.SecretKey,
44+
policy: policy,
45+
// durationSeconds: 1800,
46+
proxy: '',
47+
}, function (err, data) {
48+
if (err) {
49+
console.error(err);
50+
} else {
51+
console.log(data);
52+
var credentials = data.credentials;
53+
callback({
54+
TmpSecretId: credentials.tmpSecretId,
55+
TmpSecretKey: credentials.tmpSecretKey,
56+
XCosSecurityToken: credentials.sessionToken,
57+
ExpiredTime: data.expiredTime,
58+
ScopeLimit: true, // 设为 true 可限制密钥只在相同请求可重用,默认不限制一直可重用,细粒度控制权限需要设为 true
59+
});
60+
}
61+
});
62+
63+
},
64+
});
65+
66+
cos.putObject({
67+
Bucket: config.Bucket,
68+
Region: config.Region,
69+
Key: 'dir/1.txt',
70+
Body: 'hello!',
71+
}, function (err, data) {
72+
console.log(err || data);
73+
});

demo/demo-sts.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* 使用临时密钥例子
3+
*/
4+
var STS = require('qcloud-cos-sts');
5+
var COS = require('../index');
6+
var config = require('./config');
7+
8+
9+
var LongBucketName = config.Bucket;
10+
var ShortBucketName = LongBucketName.substr(0, LongBucketName.indexOf('-'));
11+
var AppId = LongBucketName.substr(LongBucketName.indexOf('-') + 1);
12+
var policy = {
13+
'version': '2.0',
14+
'statement': [{
15+
'action': [
16+
'name/cos:PutObject',
17+
'name/cos:InitiateMultipartUpload',
18+
'name/cos:ListMultipartUploads',
19+
'name/cos:ListParts',
20+
'name/cos:UploadPart',
21+
'name/cos:CompleteMultipartUpload'
22+
],
23+
'effect': 'allow',
24+
'principal': {'qcs': ['*']},
25+
'resource': [
26+
'qcs::cos:ap-guangzhou:uid/' + AppId + ':prefix//' + AppId + '/' + ShortBucketName + '/dir/*'
27+
]
28+
}]
29+
};
30+
31+
var cos = new COS({
32+
getAuthorization: function (options, callback) {
33+
STS.getCredential({
34+
secretId: config.SecretId,
35+
secretKey: config.SecretKey,
36+
policy: policy,
37+
durationSeconds: 7200,
38+
proxy: '',
39+
}, function (err, data) {
40+
if (err) {
41+
console.error(err);
42+
} else {
43+
var credentials = data.credentials;
44+
callback({
45+
TmpSecretId: credentials.tmpSecretId,
46+
TmpSecretKey: credentials.tmpSecretKey,
47+
XCosSecurityToken: credentials.sessionToken,
48+
ExpiredTime: data.expiredTime,
49+
});
50+
}
51+
});
52+
},
53+
});
54+
55+
cos.putObject({
56+
Bucket: config.Bucket,
57+
Region: config.Region,
58+
Key: 'dir/1.txt',
59+
Body: 'hello!',
60+
}, function (err, data) {
61+
console.log(err || data);
62+
});

demo/sts.js

Lines changed: 0 additions & 109 deletions
This file was deleted.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"name": "cos-nodejs-sdk-v5",
3-
"version": "2.4.16",
3+
"version": "2.4.17",
44
"description": "cos nodejs sdk v5",
55
"main": "index.js",
66
"scripts": {
77
"demo": "node demo/demo.js",
8+
"demo-sts": "node demo/demo-sts.js",
9+
"demo-sts-scope": "node demo/demo-sts-scope.js",
810
"test": "mocha test/test.js",
911
"csp": "mocha test/csp.js"
1012
},
@@ -27,11 +29,11 @@
2729
"homepage": "https://github.com/tencentyun/cos-nodejs-sdk-v5#readme",
2830
"dependencies": {
2931
"configstore": "^3.1.2",
30-
"qcloudapi-sdk": "^0.2.0",
3132
"request": "^2.81.0",
3233
"xml2js": "^0.4.19"
3334
},
3435
"devDependencies": {
35-
"mocha": "^4.0.1"
36+
"mocha": "^4.0.1",
37+
"qcloud-cos-sts": "^2.0.5"
3638
}
3739
}

0 commit comments

Comments
 (0)