Skip to content

Commit e44bad6

Browse files
committed
支持 UploadId 缓存,修改签名例子
1 parent a85e6c4 commit e44bad6

19 files changed

+611
-1175
lines changed

demo/demo.js

+22-22
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,33 @@ var util = {
2020
var getAuthorization = function (options, callback) {
2121

2222
// 方法一、后端通过获取临时密钥,计算签名给到前端(适用于前端调试)
23-
var method = (options.Method || 'get').toLowerCase();
24-
var key = options.Key || '';
25-
var query = options.Query || {};
26-
var headers = options.Headers || {};
27-
var pathname = key.indexOf('/') === 0 ? key : '/' + key;
2823
// var url = 'http://127.0.0.1:3000/sts';
2924
var url = '../server/sts.php';
3025
var xhr = new XMLHttpRequest();
31-
var data = {
32-
method: method,
33-
pathname: pathname,
34-
query: query,
35-
headers: headers,
36-
};
37-
xhr.open('POST', url, true);
38-
xhr.setRequestHeader('content-type', 'application/json');
26+
xhr.open('GET', url, true);
3927
xhr.onload = function (e) {
4028
try {
41-
var AuthData = JSON.parse(e.target.responseText);
29+
var data = JSON.parse(e.target.responseText);
4230
} catch (e) {
4331
}
4432
callback({
45-
Authorization: AuthData.authorization,
46-
XCosSecurityToken: AuthData.sessionToken,
33+
TmpSecretId: data.credentials && data.credentials.tmpSecretId,
34+
TmpSecretKey: data.credentials && data.credentials.tmpSecretKey,
35+
XCosSecurityToken: data.credentials && data.credentials.sessionToken,
36+
ExpiredTime: data.expiredTime,
4737
});
4838
};
49-
xhr.send(JSON.stringify(data));
39+
xhr.send();
40+
5041

51-
// // 方法二、后端计算签名(推荐
42+
// // 方法二、后端通过获取临时密钥,计算签名给到前端(适用于前端调试
5243
// var method = (options.Method || 'get').toLowerCase();
5344
// var key = options.Key || '';
5445
// var query = options.Query || {};
5546
// var headers = options.Headers || {};
5647
// var pathname = key.indexOf('/') === 0 ? key : '/' + key;
57-
// // var url = 'http://127.0.0.1:3000/auth';
58-
// var url = '../server/auth.php';
48+
// // var url = 'http://127.0.0.1:3000/sts-auth';
49+
// var url = '../server/sts-auth.php';
5950
// var xhr = new XMLHttpRequest();
6051
// var data = {
6152
// method: method,
@@ -66,10 +57,18 @@ var getAuthorization = function (options, callback) {
6657
// xhr.open('POST', url, true);
6758
// xhr.setRequestHeader('content-type', 'application/json');
6859
// xhr.onload = function (e) {
69-
// callback(e.target.responseText);
60+
// try {
61+
// var AuthData = JSON.parse(e.target.responseText);
62+
// } catch (e) {
63+
// }
64+
// callback({
65+
// Authorization: AuthData.Authorization,
66+
// XCosSecurityToken: AuthData.XCosSecurityToken,
67+
// });
7068
// };
7169
// xhr.send(JSON.stringify(data));
7270

71+
7372
// // 方法三、前端计算签名(适用于前端调试)
7473
// var authorization = COS.getAuthorization({
7574
// SecretId: 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
@@ -82,6 +81,7 @@ var getAuthorization = function (options, callback) {
8281
};
8382

8483
var cos = new COS({
84+
UploadIdCacheLimit: 3,
8585
getAuthorization: getAuthorization,
8686
});
8787
var TaskId;
@@ -595,7 +595,7 @@ function abortUploadTask() {
595595
}
596596

597597
function sliceUploadFile() {
598-
var blob = util.createFile({size: 1024 * 1024 * 2});
598+
var blob = util.createFile({size: 1024 * 1024 * 30});
599599
cos.sliceUploadFile({
600600
Bucket: config.Bucket, // Bucket 格式:test-1250000000
601601
Region: config.Region,

demo/simple-put.html

-99
This file was deleted.

demo/sts-form.html

+17-11
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,28 @@ <h1>Form 表单简单上传(兼容 IE8)</h1>
3939
// 计算签名
4040
var getAuthorization = function (options, callback) {
4141
var method = (options.Method || 'get').toLowerCase();
42-
var key = options.Key || '';
43-
// var url = 'http://127.0.0.1:3000/sts-post-object' +
44-
var url = '../server/sts-post-object.php' +
42+
// var url = 'http://127.0.0.1:3000/sts-auth' +
43+
var url = '../server/sts-auth.php' +
4544
'?method=' + method +
46-
'&pathname=' + encodeURIComponent('/') +
47-
'&key=' + encodeURIComponent(key);
45+
'&pathname=' + encodeURIComponent('/');
4846
var xhr = new XMLHttpRequest();
4947
xhr.open('GET', url, true);
5048
xhr.onreadystatechange = function (e) {
5149
if (xhr.readyState === 4) {
5250
if (xhr.status === 200) {
53-
var data = JSON.parse(xhr.responseText);
54-
callback(null, {
55-
Authorization: data.authorization,
56-
XCosSecurityToken: data.sessionToken,
57-
});
51+
var AuthData;
52+
try {
53+
AuthData = (new Function('return ' + xhr.responseText))();
54+
} catch (e) {}
55+
if (AuthData && AuthData.Authorization) {
56+
callback(null, {
57+
Authorization: AuthData.Authorization,
58+
XCosSecurityToken: AuthData.XCosSecurityToken,
59+
});
60+
} else {
61+
console.error(AuthData);
62+
callback('获取签名出错');
63+
}
5864
} else {
5965
callback('获取签名出错');
6066
}
@@ -102,7 +108,7 @@ <h1>Form 表单简单上传(兼容 IE8)</h1>
102108
document.getElementById('success_action_redirect').value = location.href.substr(0, location.href.lastIndexOf('/') + 1) + 'empty.html';
103109
document.getElementById('key').value = Key;
104110
document.getElementById('Signature').value = AuthData.Authorization;
105-
document.getElementById('x-cos-security-token').value = AuthData.XCosSecurityToken;
111+
document.getElementById('x-cos-security-token').value = AuthData.XCosSecurityToken || '';
106112
form.submit();
107113
});
108114
};

demo/sts-post.html

+18-19
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,26 @@ <h1>Ajax Post 上传</h1>
3333

3434
// 计算签名
3535
var getAuthorization = function (options, callback) {
36-
var method = (options.Method || 'get').toLowerCase();
37-
var key = options.Key || '';
38-
// var url = 'http://127.0.0.1:3000/sts-post-object' +
39-
var url = '../server/sts-post-object.php' +
40-
'?method=' + method +
41-
'&pathname=' + encodeURIComponent('/') +
42-
'&key=' + encodeURIComponent(key);
36+
// var url = 'http://127.0.0.1:3000/sts-auth' +
37+
var url = '../server/sts-auth.php' +
38+
'?method=post' +
39+
'&pathname=' + encodeURIComponent('/');
4340
var xhr = new XMLHttpRequest();
4441
xhr.open('GET', url, true);
4542
xhr.onload = function (e) {
46-
var data = JSON.parse(e.target.responseText);
47-
if (data.authorization === '') {
48-
43+
var AuthData;
44+
try {
45+
AuthData = JSON.parse(xhr.responseText)
46+
} catch (e) {}
47+
if (AuthData && AuthData.Authorization) {
48+
callback(null, {
49+
Authorization: AuthData.Authorization,
50+
XCosSecurityToken: AuthData.XCosSecurityToken,
51+
});
52+
} else {
53+
console.error(AuthData);
54+
callback('获取签名出错');
4955
}
50-
callback(null, {
51-
Authorization: data.authorization,
52-
XCosSecurityToken: data.sessionToken,
53-
});
5456
};
5557
xhr.onerror = function (e) {
5658
callback('获取签名出错');
@@ -63,14 +65,11 @@ <h1>Ajax Post 上传</h1>
6365
var Key = 'dir/' + file.name; // 这里指定上传目录和文件名
6466

6567
getAuthorization({Method: 'POST', Key: Key}, function (err, info) {
66-
var auth = info.Authorization;
67-
var XCosSecurityToken = info.XCosSecurityToken;
68-
6968
var fd = new FormData();
7069
fd.append('key', Key);
71-
fd.append('Signature', auth);
70+
fd.append('Signature', info.Authorization);
7271
fd.append('Content-Type', '');
73-
XCosSecurityToken && fd.append('x-cos-security-token', XCosSecurityToken);
72+
info.XCosSecurityToken && fd.append('x-cos-security-token', info.XCosSecurityToken);
7473
fd.append('file', file);
7574
var url = prefix;
7675
var xhr = new XMLHttpRequest();

demo/sts-put.html

+22-18
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,32 @@ <h1>Ajax Put 上传</h1>
3535
var getAuthorization = function (options, callback) {
3636
var method = (options.Method || 'get').toLowerCase();
3737
var key = options.Key || '';
38-
var query = options.Query || {};
39-
var headers = options.Headers || {};
4038
var pathname = key.indexOf('/') === 0 ? key : '/' + key;
41-
// var url = 'http://127.0.0.1:3000/auth';
42-
var url = '../server/sts.php';
39+
// var url = 'http://127.0.0.1:3000/sts-auth' +
40+
var url = '../server/sts-auth.php' +
41+
'?method=' + method +
42+
'&pathname=' + encodeURIComponent(pathname);
4343
var xhr = new XMLHttpRequest();
44-
var data = {
45-
method: method,
46-
pathname: pathname,
47-
query: query,
48-
headers: headers,
49-
};
50-
xhr.open('POST', url, true);
51-
xhr.setRequestHeader('content-type', 'application/json');
44+
xhr.open('GET', url, true);
5245
xhr.onload = function (e) {
53-
var data = JSON.parse(e.target.responseText);
54-
callback(data.error, {
55-
Authorization: data.authorization,
56-
XCosSecurityToken: data.credentials && data.credentials.sessionToken,
57-
});
46+
var AuthData;
47+
try {
48+
AuthData = JSON.parse(xhr.responseText)
49+
} catch (e) {}
50+
if (AuthData && AuthData.Authorization) {
51+
callback(null, {
52+
Authorization: AuthData.Authorization,
53+
XCosSecurityToken: AuthData.XCosSecurityToken,
54+
});
55+
} else {
56+
console.error(AuthData);
57+
callback('获取签名出错');
58+
}
59+
};
60+
xhr.onerror = function (e) {
61+
callback('获取签名出错');
5862
};
59-
xhr.send(JSON.stringify(data));
63+
xhr.send();
6064
};
6165

6266
// 上传文件

0 commit comments

Comments
 (0)