-
Notifications
You must be signed in to change notification settings - Fork 49
/
demo-sts.js
67 lines (63 loc) · 1.98 KB
/
demo-sts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* 使用临时密钥例子
*/
var STS = require('qcloud-cos-sts');
var COS = require('../index');
var config = require('./config');
var LongBucketName = config.Bucket;
var ShortBucketName = LongBucketName.substr(0, LongBucketName.lastIndexOf('-'));
var AppId = LongBucketName.substr(LongBucketName.lastIndexOf('-') + 1);
var policy = {
'version': '2.0',
'statement': [{
'action': [
// 所有 action 请看文档 https://cloud.tencent.com/document/product/436/31923
// 简单上传
'name/cos:PutObject',
'name/cos:PostObject',
// 分片上传
'name/cos:InitiateMultipartUpload',
'name/cos:ListMultipartUploads',
'name/cos:ListParts',
'name/cos:UploadPart',
'name/cos:CompleteMultipartUpload'
],
'effect': 'allow',
'principal': {'qcs': ['*']},
'resource': [
'qcs::cos:' + config.Region + ':uid/' + AppId + ':prefix//' + AppId + '/' + ShortBucketName + '/*'
]
}]
};
var cos = new COS({
getAuthorization: function (options, callback) {
STS.getCredential({
secretId: config.SecretId,
secretKey: config.SecretKey,
policy: policy,
durationSeconds: 7200,
proxy: '',
region: 'ap-guangzhou'
}, function (err, data) {
if (err) {
console.error(err);
} else {
var credentials = data.credentials;
callback({
TmpSecretId: credentials.tmpSecretId,
TmpSecretKey: credentials.tmpSecretKey,
XCosSecurityToken: credentials.sessionToken,
ExpiredTime: data.expiredTime,
});
}
});
},
});
cos.putObject({
Bucket: config.Bucket,
Region: config.Region,
Key: 'dir/1.txt',
Body: 'hello!',
}, function (err, data) {
console.log(err || data);
});