Skip to content

Commit ac68b11

Browse files
authored
fix: 优化初始化用法 (#212)
1 parent 50893bb commit ac68b11

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed

dist/cos-js-sdk-v5.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12271,6 +12271,12 @@ function getAuthorizationAsync(params, callback) {
1227112271
} else {
1227212272
// 内部计算获取签名
1227312273
return function () {
12274+
var KeyTime = '';
12275+
if (self.options.StartTime && params.Expires) {
12276+
KeyTime = self.options.StartTime + ';' + (self.options.StartTime + params.Expires * 1);
12277+
} else if (self.options.StartTime && self.options.ExpiredTime) {
12278+
KeyTime = self.options.StartTime + ';' + self.options.ExpiredTime;
12279+
}
1227412280
var Authorization = util.getAuth({
1227512281
SecretId: params.SecretId || self.options.SecretId,
1227612282
SecretKey: params.SecretKey || self.options.SecretKey,
@@ -12279,6 +12285,7 @@ function getAuthorizationAsync(params, callback) {
1227912285
Query: params.Query,
1228012286
Headers: headers,
1228112287
Expires: params.Expires,
12288+
KeyTime: KeyTime,
1228212289
UseRawKey: self.options.UseRawKey,
1228312290
SystemClockOffset: self.options.SystemClockOffset,
1228412291
ForceSignHost: forceSignHost
@@ -12830,6 +12837,10 @@ var defaultOptions = {
1283012837
SecretKey: '',
1283112838
SecurityToken: '',
1283212839
// 使用临时密钥需要注意自行刷新 Token
12840+
StartTime: 0,
12841+
// 临时密钥返回起始时间
12842+
ExpiredTime: 0,
12843+
// 临时密钥过期时间
1283312844
ChunkRetryTimes: 2,
1283412845
FileParallelLimit: 3,
1283512846
ChunkParallelLimit: 3,

dist/cos-js-sdk-v5.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ declare namespace COS {
138138
SecurityToken?: string;
139139
/** 同 SecurityToken,推荐用 SecurityToken */
140140
XCosSecurityToken?: string;
141+
/** 临时密钥起始时间 */
142+
StartTime?: number;
143+
/** 临时密钥过期时间 */
144+
ExpiredTime?: number;
141145
/** 分块上传及分块复制时,出错重试次数,默认值3(加第一次,请求共4次) */
142146
ChunkRetryTimes?: number;
143147
/** 同一个实例下上传的文件并发数,默认值3 */

server/sts.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,21 @@ app.all('/', (req, res, next) => res.redirect('/demo/'));
143143
app.use(bodyParser.json());
144144

145145
// 获取临时密钥
146-
function getSts() {
146+
function getSts(key) {
147147
return new Promise((resolve, reject) => {
148148
// 获取临时密钥
149149
var AppId = config.bucket.substr(config.bucket.lastIndexOf('-') + 1);
150150
// 数据万象DescribeMediaBuckets接口需要resource为*,参考 https://cloud.tencent.com/document/product/460/41741
151+
var resource =
152+
'qcs::cos:' +
153+
config.region +
154+
':uid/' +
155+
AppId +
156+
':' +
157+
config.bucket +
158+
'/' +
159+
config.allowPrefix +
160+
`${key} ? /${key} : ''`;
151161
var policy = {
152162
version: '2.0',
153163
statement: [
@@ -156,7 +166,7 @@ function getSts() {
156166
effect: 'allow',
157167
resource: [
158168
// cos相关授权路径
159-
'qcs::cos:' + config.region + ':uid/' + AppId + ':' + config.bucket + '/' + config.allowPrefix,
169+
resource,
160170
// ci相关授权路径 按需使用
161171
'qcs::ci:' + config.region + ':uid/' + AppId + ':bucket/' + config.bucket + '/' + 'job/*',
162172
],
@@ -187,7 +197,7 @@ function getSts() {
187197
}
188198

189199
// 返回临时密钥,客户端自行计算签名
190-
app.all('/sts', function (req, res, next) {
200+
app.get('/sts', function (req, res, next) {
191201
// TODO 这里根据自己业务需要做好放行判断
192202
if (config.allowPrefix === '_ALLOW_DIR_/*') {
193203
res.send({ error: '请修改 allowPrefix 配置项,指定允许上传的路径前缀' });
@@ -202,6 +212,34 @@ app.all('/sts', function (req, res, next) {
202212
});
203213
});
204214

215+
// 返回临时密钥和上传信息,客户端自行计算签名
216+
app.get('/getKeyAndCredentials', function (req, res, next) {
217+
// TODO 这里根据自己业务需要做好放行判断
218+
if (config.allowPrefix === '_ALLOW_DIR_/*') {
219+
res.send({ error: '请修改 allowPrefix 配置项,指定允许上传的路径前缀' });
220+
return;
221+
}
222+
var ext = req.query.ext;
223+
var cosKey = generateCosKey(ext);
224+
// 可控制临时密钥只授权cosKey的权限
225+
getSts(cosKey)
226+
.then((data) => {
227+
res.send({
228+
TmpSecretId: data.credentials.tmpSecretId,
229+
TmpSecretKey: data.credentials.tmpSecretKey,
230+
SessionToken: data.credentials.sessionToken,
231+
StartTime: Math.round(Date.now() / 1000),
232+
ExpiredTime: data.expiredTime,
233+
Bucket: config.bucket,
234+
Region: config.region,
235+
Key: cosKey,
236+
});
237+
})
238+
.catch((err) => {
239+
res.send(err);
240+
});
241+
});
242+
205243
// // 格式二:临时密钥接口,支持细粒度权限控制
206244
// // 判断是否允许获取密钥
207245
// var allowScope = function (scope) {

src/base.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3894,6 +3894,12 @@ function getAuthorizationAsync(params, callback) {
38943894
} else {
38953895
// 内部计算获取签名
38963896
return (function () {
3897+
var KeyTime = '';
3898+
if (self.options.StartTime && params.Expires) {
3899+
KeyTime = self.options.StartTime + ';' + (self.options.StartTime + params.Expires * 1);
3900+
} else if (self.options.StartTime && self.options.ExpiredTime) {
3901+
KeyTime = self.options.StartTime + ';' + self.options.ExpiredTime;
3902+
}
38973903
var Authorization = util.getAuth({
38983904
SecretId: params.SecretId || self.options.SecretId,
38993905
SecretKey: params.SecretKey || self.options.SecretKey,
@@ -3902,6 +3908,7 @@ function getAuthorizationAsync(params, callback) {
39023908
Query: params.Query,
39033909
Headers: headers,
39043910
Expires: params.Expires,
3911+
KeyTime,
39053912
UseRawKey: self.options.UseRawKey,
39063913
SystemClockOffset: self.options.SystemClockOffset,
39073914
ForceSignHost: forceSignHost,

src/cos.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ var defaultOptions = {
1212
SecretId: '',
1313
SecretKey: '',
1414
SecurityToken: '', // 使用临时密钥需要注意自行刷新 Token
15+
StartTime: 0, // 临时密钥返回起始时间
16+
ExpiredTime: 0, // 临时密钥过期时间
1517
ChunkRetryTimes: 2,
1618
FileParallelLimit: 3,
1719
ChunkParallelLimit: 3,

0 commit comments

Comments
 (0)