Skip to content

Commit

Permalink
v1.2.4 例子里 XCosSecurityToken 改为 SecurityToken
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Feb 26, 2021
1 parent c0c1696 commit dda5aa8
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 21 deletions.
14 changes: 10 additions & 4 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,18 @@ var getAuthorization = function (options, callback) {
} catch (e) {
}
if (!data || !credentials) return console.error('credentials invalid');
var authorization = COS.getAuthorization({
SecretId: credentials.tmpSecretId, // 可传固定密钥或者临时密钥
SecretKey: credentials.tmpSecretKey, // 可传固定密钥或者临时密钥
Method: options.Method,
Pathname: options.Pathname,
Query: options.Query,
Headers: options.Headers,
Expires: 900,
});
callback({
TmpSecretId: credentials.tmpSecretId,
TmpSecretKey: credentials.tmpSecretKey,
Authorization: authorization,
XCosSecurityToken: credentials.sessionToken,
StartTime: data.startTime, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
ExpiredTime: data.expiredTime, // 时间戳,单位秒,如:1580000900
});
};
xhr.send();
Expand Down
2 changes: 1 addition & 1 deletion demo/policy-post.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ <h1>PostObject 上传(Policy 保护,Ajax POST 请求)</h1>

// // 使用普通签名格式
// fd.append('Signature', credentials.Authorization);
// fd.append('x-cos-security-token', credentials.XCosSecurityToken || '');
// fd.append('x-cos-security-token', credentials.SecurityToken || '');

// 使用 policy 签名保护格式
credentials.securityToken && fd.append('x-cos-security-token', credentials.securityToken);
Expand Down
2 changes: 1 addition & 1 deletion demo/queue/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var cos = new COS({
callback({
TmpSecretId: data.credentials && data.credentials.tmpSecretId,
TmpSecretKey: data.credentials && data.credentials.tmpSecretKey,
XCosSecurityToken: data.credentials && data.credentials.sessionToken,
SecurityToken: data.credentials && data.credentials.sessionToken,
ExpiredTime: data.expiredTime
});
};
Expand Down
2 changes: 1 addition & 1 deletion demo/slice.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ <h1>自定义的分片上传</h1>
callback({
TmpSecretId: credentials.tmpSecretId,
TmpSecretKey: credentials.tmpSecretKey,
XCosSecurityToken: credentials.sessionToken,
SecurityToken: credentials.sessionToken,
StartTime: data.startTime, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
ExpiredTime: data.expiredTime, // 时间戳,单位秒,如:1580000900
});
Expand Down
4 changes: 2 additions & 2 deletions demo/sts-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ <h1>Form 表单简单上传(兼容 IE8)</h1>
} catch (e) {}
if (credentials) {
callback(null, {
XCosSecurityToken: credentials.sessionToken,
SecurityToken: credentials.sessionToken,
Authorization: CosAuth({
SecretId: credentials.tmpSecretId,
SecretKey: credentials.tmpSecretKey,
Expand Down Expand Up @@ -123,7 +123,7 @@ <h1>Form 表单简单上传(兼容 IE8)</h1>
document.getElementById('success_action_redirect').value = location.href.substr(0, location.href.lastIndexOf('/') + 1) + 'empty.html';
document.getElementById('key').value = Key;
document.getElementById('signature').value = AuthData.Authorization;
document.getElementById('x-cos-security-token').value = AuthData.XCosSecurityToken || '';
document.getElementById('x-cos-security-token').value = AuthData.SecurityToken || '';
form.submit();
});
};
Expand Down
4 changes: 2 additions & 2 deletions demo/sts-post.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h1>Ajax Post 上传</h1>
} catch (e) {}
if (credentials) {
callback(null, {
XCosSecurityToken: credentials.sessionToken,
SecurityToken: credentials.sessionToken,
Authorization: CosAuth({
SecretId: credentials.tmpSecretId,
SecretKey: credentials.tmpSecretKey,
Expand Down Expand Up @@ -84,7 +84,7 @@ <h1>Ajax Post 上传</h1>
fd.append('key', Key);
fd.append('signature', info.Authorization);
fd.append('Content-Type', '');
info.XCosSecurityToken && fd.append('x-cos-security-token', info.XCosSecurityToken);
info.SecurityToken && fd.append('x-cos-security-token', info.SecurityToken);
fd.append('file', file); // file 字段放在表单最后,避免文件内容过长影响签名判断和鉴权
var url = prefix;
var xhr = new XMLHttpRequest();
Expand Down
6 changes: 3 additions & 3 deletions demo/sts-put-server-key.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h1>Ajax Put 上传</h1>
if (result && credentials) {
callback(null, {
Key: result.Key, // 这里在后端加一个字段决定上传的文件名
XCosSecurityToken: credentials.sessionToken,
SecurityToken: credentials.sessionToken,
Authorization: CosAuth({
SecretId: credentials.tmpSecretId,
SecretKey: credentials.tmpSecretKey,
Expand Down Expand Up @@ -86,12 +86,12 @@ <h1>Ajax Put 上传</h1>
}

var auth = info.Authorization;
var XCosSecurityToken = info.XCosSecurityToken;
var SecurityToken = info.SecurityToken;
var url = prefix + camSafeUrlEncode(Key).replace(/%2F/g, '/');
var xhr = new XMLHttpRequest();
xhr.open('PUT', url, true);
xhr.setRequestHeader('Authorization', auth);
XCosSecurityToken && xhr.setRequestHeader('x-cos-security-token', XCosSecurityToken);
SecurityToken && xhr.setRequestHeader('x-cos-security-token', SecurityToken);
xhr.upload.onprogress = function (e) {
console.log('上传进度 ' + (Math.round(e.loaded / e.total * 10000) / 100) + '%');
};
Expand Down
6 changes: 3 additions & 3 deletions demo/sts-put.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h1>Ajax Put 上传</h1>
} catch (e) {}
if (credentials) {
callback(null, {
XCosSecurityToken: credentials.sessionToken,
SecurityToken: credentials.sessionToken,
Authorization: CosAuth({
SecretId: credentials.tmpSecretId,
SecretKey: credentials.tmpSecretKey,
Expand Down Expand Up @@ -85,12 +85,12 @@ <h1>Ajax Put 上传</h1>
}

var auth = info.Authorization;
var XCosSecurityToken = info.XCosSecurityToken;
var SecurityToken = info.SecurityToken;
var url = prefix + camSafeUrlEncode(Key).replace(/%2F/g, '/');
var xhr = new XMLHttpRequest();
xhr.open('PUT', url, true);
xhr.setRequestHeader('Authorization', auth);
XCosSecurityToken && xhr.setRequestHeader('x-cos-security-token', XCosSecurityToken);
SecurityToken && xhr.setRequestHeader('x-cos-security-token', SecurityToken);
xhr.upload.onprogress = function (e) {
console.log('上传进度 ' + (Math.round(e.loaded / e.total * 10000) / 100) + '%');
};
Expand Down
7 changes: 6 additions & 1 deletion dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -2221,7 +2221,7 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '1.2.3';
COS.version = '1.2.4';

module.exports = COS;

Expand Down Expand Up @@ -7772,6 +7772,11 @@ function getAuthorizationAsync(params, callback) {
var cb = function (err, AuthData) {
if (cbDone) return;
cbDone = true;
if (AuthData.XCosSecurityToken && !AuthData.SecurityToken) {
AuthData = util.clone(AuthData);
AuthData.SecurityToken = AuthData.XCosSecurityToken;
delete AuthData.XCosSecurityToken;
}
callback && callback(err, AuthData);
};

Expand Down
2 changes: 1 addition & 1 deletion dist/cos-js-sdk-v5.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cos-js-sdk-v5",
"version": "1.2.3",
"version": "1.2.4",
"description": "JavaScript SDK for [腾讯云对象存储](https://cloud.tencent.com/product/cos)",
"main": "index.js",
"types": "types",
Expand Down
5 changes: 5 additions & 0 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2981,6 +2981,11 @@ function getAuthorizationAsync(params, callback) {
var cb = function (err, AuthData) {
if (cbDone) return;
cbDone = true;
if (AuthData.XCosSecurityToken && !AuthData.SecurityToken) {
AuthData = util.clone(AuthData);
AuthData.SecurityToken = AuthData.XCosSecurityToken;
delete AuthData.XCosSecurityToken;
}
callback && callback(err, AuthData);
};

Expand Down
2 changes: 1 addition & 1 deletion src/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '1.2.3';
COS.version = '1.2.4';

module.exports = COS;

0 comments on commit dda5aa8

Please sign in to comment.