Skip to content

Commit

Permalink
支持 UploadId 缓存,修改签名例子
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Apr 18, 2018
1 parent a85e6c4 commit e44bad6
Show file tree
Hide file tree
Showing 19 changed files with 611 additions and 1,175 deletions.
44 changes: 22 additions & 22 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,33 @@ var util = {
var getAuthorization = function (options, callback) {

// 方法一、后端通过获取临时密钥,计算签名给到前端(适用于前端调试)
var method = (options.Method || 'get').toLowerCase();
var key = options.Key || '';
var query = options.Query || {};
var headers = options.Headers || {};
var pathname = key.indexOf('/') === 0 ? key : '/' + key;
// var url = 'http://127.0.0.1:3000/sts';
var url = '../server/sts.php';
var xhr = new XMLHttpRequest();
var data = {
method: method,
pathname: pathname,
query: query,
headers: headers,
};
xhr.open('POST', url, true);
xhr.setRequestHeader('content-type', 'application/json');
xhr.open('GET', url, true);
xhr.onload = function (e) {
try {
var AuthData = JSON.parse(e.target.responseText);
var data = JSON.parse(e.target.responseText);
} catch (e) {
}
callback({
Authorization: AuthData.authorization,
XCosSecurityToken: AuthData.sessionToken,
TmpSecretId: data.credentials && data.credentials.tmpSecretId,
TmpSecretKey: data.credentials && data.credentials.tmpSecretKey,
XCosSecurityToken: data.credentials && data.credentials.sessionToken,
ExpiredTime: data.expiredTime,
});
};
xhr.send(JSON.stringify(data));
xhr.send();


// // 方法二、后端计算签名(推荐
// // 方法二、后端通过获取临时密钥,计算签名给到前端(适用于前端调试
// var method = (options.Method || 'get').toLowerCase();
// var key = options.Key || '';
// var query = options.Query || {};
// var headers = options.Headers || {};
// var pathname = key.indexOf('/') === 0 ? key : '/' + key;
// // var url = 'http://127.0.0.1:3000/auth';
// var url = '../server/auth.php';
// // var url = 'http://127.0.0.1:3000/sts-auth';
// var url = '../server/sts-auth.php';
// var xhr = new XMLHttpRequest();
// var data = {
// method: method,
Expand All @@ -66,10 +57,18 @@ var getAuthorization = function (options, callback) {
// xhr.open('POST', url, true);
// xhr.setRequestHeader('content-type', 'application/json');
// xhr.onload = function (e) {
// callback(e.target.responseText);
// try {
// var AuthData = JSON.parse(e.target.responseText);
// } catch (e) {
// }
// callback({
// Authorization: AuthData.Authorization,
// XCosSecurityToken: AuthData.XCosSecurityToken,
// });
// };
// xhr.send(JSON.stringify(data));


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

var cos = new COS({
UploadIdCacheLimit: 3,
getAuthorization: getAuthorization,
});
var TaskId;
Expand Down Expand Up @@ -595,7 +595,7 @@ function abortUploadTask() {
}

function sliceUploadFile() {
var blob = util.createFile({size: 1024 * 1024 * 2});
var blob = util.createFile({size: 1024 * 1024 * 30});
cos.sliceUploadFile({
Bucket: config.Bucket, // Bucket 格式:test-1250000000
Region: config.Region,
Expand Down
99 changes: 0 additions & 99 deletions demo/simple-put.html

This file was deleted.

28 changes: 17 additions & 11 deletions demo/sts-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,28 @@ <h1>Form 表单简单上传(兼容 IE8)</h1>
// 计算签名
var getAuthorization = function (options, callback) {
var method = (options.Method || 'get').toLowerCase();
var key = options.Key || '';
// var url = 'http://127.0.0.1:3000/sts-post-object' +
var url = '../server/sts-post-object.php' +
// var url = 'http://127.0.0.1:3000/sts-auth' +
var url = '../server/sts-auth.php' +
'?method=' + method +
'&pathname=' + encodeURIComponent('/') +
'&key=' + encodeURIComponent(key);
'&pathname=' + encodeURIComponent('/');
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var data = JSON.parse(xhr.responseText);
callback(null, {
Authorization: data.authorization,
XCosSecurityToken: data.sessionToken,
});
var AuthData;
try {
AuthData = (new Function('return ' + xhr.responseText))();
} catch (e) {}
if (AuthData && AuthData.Authorization) {
callback(null, {
Authorization: AuthData.Authorization,
XCosSecurityToken: AuthData.XCosSecurityToken,
});
} else {
console.error(AuthData);
callback('获取签名出错');
}
} else {
callback('获取签名出错');
}
Expand Down Expand Up @@ -102,7 +108,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.XCosSecurityToken || '';
form.submit();
});
};
Expand Down
37 changes: 18 additions & 19 deletions demo/sts-post.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,26 @@ <h1>Ajax Post 上传</h1>

// 计算签名
var getAuthorization = function (options, callback) {
var method = (options.Method || 'get').toLowerCase();
var key = options.Key || '';
// var url = 'http://127.0.0.1:3000/sts-post-object' +
var url = '../server/sts-post-object.php' +
'?method=' + method +
'&pathname=' + encodeURIComponent('/') +
'&key=' + encodeURIComponent(key);
// var url = 'http://127.0.0.1:3000/sts-auth' +
var url = '../server/sts-auth.php' +
'?method=post' +
'&pathname=' + encodeURIComponent('/');
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function (e) {
var data = JSON.parse(e.target.responseText);
if (data.authorization === '') {

var AuthData;
try {
AuthData = JSON.parse(xhr.responseText)
} catch (e) {}
if (AuthData && AuthData.Authorization) {
callback(null, {
Authorization: AuthData.Authorization,
XCosSecurityToken: AuthData.XCosSecurityToken,
});
} else {
console.error(AuthData);
callback('获取签名出错');
}
callback(null, {
Authorization: data.authorization,
XCosSecurityToken: data.sessionToken,
});
};
xhr.onerror = function (e) {
callback('获取签名出错');
Expand All @@ -63,14 +65,11 @@ <h1>Ajax Post 上传</h1>
var Key = 'dir/' + file.name; // 这里指定上传目录和文件名

getAuthorization({Method: 'POST', Key: Key}, function (err, info) {
var auth = info.Authorization;
var XCosSecurityToken = info.XCosSecurityToken;

var fd = new FormData();
fd.append('key', Key);
fd.append('Signature', auth);
fd.append('Signature', info.Authorization);
fd.append('Content-Type', '');
XCosSecurityToken && fd.append('x-cos-security-token', XCosSecurityToken);
info.XCosSecurityToken && fd.append('x-cos-security-token', info.XCosSecurityToken);
fd.append('file', file);
var url = prefix;
var xhr = new XMLHttpRequest();
Expand Down
40 changes: 22 additions & 18 deletions demo/sts-put.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,32 @@ <h1>Ajax Put 上传</h1>
var getAuthorization = function (options, callback) {
var method = (options.Method || 'get').toLowerCase();
var key = options.Key || '';
var query = options.Query || {};
var headers = options.Headers || {};
var pathname = key.indexOf('/') === 0 ? key : '/' + key;
// var url = 'http://127.0.0.1:3000/auth';
var url = '../server/sts.php';
// var url = 'http://127.0.0.1:3000/sts-auth' +
var url = '../server/sts-auth.php' +
'?method=' + method +
'&pathname=' + encodeURIComponent(pathname);
var xhr = new XMLHttpRequest();
var data = {
method: method,
pathname: pathname,
query: query,
headers: headers,
};
xhr.open('POST', url, true);
xhr.setRequestHeader('content-type', 'application/json');
xhr.open('GET', url, true);
xhr.onload = function (e) {
var data = JSON.parse(e.target.responseText);
callback(data.error, {
Authorization: data.authorization,
XCosSecurityToken: data.credentials && data.credentials.sessionToken,
});
var AuthData;
try {
AuthData = JSON.parse(xhr.responseText)
} catch (e) {}
if (AuthData && AuthData.Authorization) {
callback(null, {
Authorization: AuthData.Authorization,
XCosSecurityToken: AuthData.XCosSecurityToken,
});
} else {
console.error(AuthData);
callback('获取签名出错');
}
};
xhr.onerror = function (e) {
callback('获取签名出错');
};
xhr.send(JSON.stringify(data));
xhr.send();
};

// 上传文件
Expand Down
Loading

0 comments on commit e44bad6

Please sign in to comment.