Skip to content

Commit

Permalink
优化例子指引
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Jan 11, 2019
1 parent 4131313 commit e30d407
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 26 deletions.
29 changes: 17 additions & 12 deletions demo/demo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var config = {
Bucket: 'test-1251902136',
Bucket: 'test-1250000000',
Region: 'ap-guangzhou'
};

Expand Down Expand Up @@ -29,7 +29,7 @@ var camSafeUrlEncode = function (str) {

var getAuthorization = function (options, callback) {

// 方法一、(推荐)后端通过获取临时密钥给到前端,前端计算签名
// 格式一、(推荐)后端通过获取临时密钥给到前端,前端计算签名
// 服务端 JS 和 PHP 例子:https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/
// 服务端其他语言参考 COS STS SDK :https://github.com/tencentyun/qcloud-cos-sts-sdk
// var url = 'http://127.0.0.1:3000/sts';
Expand All @@ -46,13 +46,13 @@ var getAuthorization = function (options, callback) {
TmpSecretId: credentials.tmpSecretId,
TmpSecretKey: credentials.tmpSecretKey,
XCosSecurityToken: credentials.sessionToken,
ExpiredTime: data.expiredTime,
ExpiredTime: data.expiredTime, // SDK 在 ExpiredTime 时间前,不会再次调用 getAuthorization
});
};
xhr.send();


// // 方法二、(推荐)【细粒度控制权限】后端通过获取临时密钥给到前端,前端只有相同请求才重用临时密钥,后端可以通过 Scope 细粒度控制权限
// // 格式二、(推荐)【细粒度控制权限】后端通过获取临时密钥给到前端,前端只有相同请求才重用临时密钥,后端可以通过 Scope 细粒度控制权限
// // 服务端例子:https://github.com/tencentyun/qcloud-cos-sts-sdk/edit/master/scope.md
// var url = 'http://127.0.0.1:3000/sts-scope';
// var xhr = new XMLHttpRequest();
Expand All @@ -75,8 +75,9 @@ var getAuthorization = function (options, callback) {
// xhr.send(JSON.stringify(options.Scope));


// //方法三、(不推荐,分片上传权限不好控制)后端使用固定密钥计算签名,返回给前端,auth.php,注意:这种有安全风险,后端需要通过 method、pathname 控制好权限,比如不允许 put / 等,这里暂不提供
// 服务端获取签名,请参考对应语言的 COS SDK:https://cloud.tencent.com/document/product/436/6474
// // 格式三、(不推荐,分片上传权限不好控制)前端每次请求前都需要通过 getAuthorization 获取签名,后端使用固定密钥或临时密钥计算签名返回给前端
// // 服务端获取签名,请参考对应语言的 COS SDK:https://cloud.tencent.com/document/product/436/6474
// // 注意:这种有安全风险,后端需要通过 method、pathname 严格控制好权限,比如不允许 put / 等
// var method = (options.Method || 'get').toLowerCase();
// var query = options.Query || {};
// var headers = options.Headers || {};
Expand All @@ -93,20 +94,24 @@ var getAuthorization = function (options, callback) {
// xhr.open('POST', url, true);
// xhr.setRequestHeader('content-type', 'application/json');
// xhr.onload = function (e) {
// try {
// var data = JSON.parse(e.target.responseText);
// } catch (e) {
// }
// callback({
// Authorization: e.target.responseText,
// // XCosSecurityToken: sessionToken, // 如果使用临时密钥,需要传 sessionToken
// Authorization: data.authorization,
// // XCosSecurityToken: data.sessionToken, // 如果使用临时密钥,需要把 sessionToken 传给 XCosSecurityToken
// });
// };
// xhr.send(JSON.stringify(data));


// //方法四、(不推荐,适用于前端调试)前端使用固定密钥计算签名
// // 格式四、(不推荐,适用于前端调试,避免泄露密钥)前端使用固定密钥计算签名
// var authorization = COS.getAuthorization({
// SecretId: 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// SecretId: 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // 可传固定密钥或者临时密钥
// SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', // 可传固定密钥或者临时密钥
// Method: options.Method,
// Key: options.Key,
// Pathname: options.Pathname,
// Query: options.Query,
// Headers: options.Headers,
// Expires: 900,
Expand Down
82 changes: 68 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,47 +38,100 @@ function camSafeUrlEncode(str) {

var getAuthorization = function (options, callback) {


// 方法一、后端通过获取临时密钥给到前端,前端计算签名
// 格式一、(推荐)后端通过获取临时密钥给到前端,前端计算签名
// 服务端 JS 和 PHP 例子:https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/
// 服务端其他语言参考 COS STS SDK :https://github.com/tencentyun/qcloud-cos-sts-sdk
// var url = 'http://127.0.0.1:3000/sts';
var url = '../server/sts.php';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function (e) {
try {
var data = JSON.parse(e.target.responseText);
var credentials = data.credentials;
} catch (e) {
}
callback({
TmpSecretId: data.credentials && data.credentials.tmpSecretId,
TmpSecretKey: data.credentials && data.credentials.tmpSecretKey,
XCosSecurityToken: data.credentials && data.credentials.sessionToken,
ExpiredTime: data.expiredTime,
TmpSecretId: credentials.tmpSecretId,
TmpSecretKey: credentials.tmpSecretKey,
XCosSecurityToken: credentials.sessionToken,
ExpiredTime: data.expiredTime, // SDK 在 ExpiredTime 时间前,不会再次调用 getAuthorization
});
};
xhr.send();


// // 方法二、后端通过获取临时密钥给到前端,前端计算签名
// var url = 'http://127.0.0.1:3000/sts';
// // 格式二、(推荐)【细粒度控制权限】后端通过获取临时密钥给到前端,前端只有相同请求才重用临时密钥,后端可以通过 Scope 细粒度控制权限
// // 服务端例子:https://github.com/tencentyun/qcloud-cos-sts-sdk/edit/master/scope.md
// var url = 'http://127.0.0.1:3000/sts-scope';
// var xhr = new XMLHttpRequest();
// xhr.open('POST', url, true);
// xhr.setRequestHeader('Content-Type', 'application/json');
// xhr.onload = function (e) {
// var data = {};
// try {
// data = JSON.parse(e.target.responseText);
// var data = JSON.parse(e.target.responseText);
// var credentials = data.credentials;
// } catch (e) {
// }
// callback({
// TmpSecretId: data.credentials && data.credentials.tmpSecretId,
// TmpSecretKey: data.credentials && data.credentials.tmpSecretKey,
// XCosSecurityToken: data.credentials && data.credentials.sessionToken,
// TmpSecretId: credentials.tmpSecretId,
// TmpSecretKey: credentials.tmpSecretKey,
// XCosSecurityToken: credentials.sessionToken,
// ExpiredTime: data.expiredTime,
// ScopeLimit: true,
// ScopeLimit: true, // 设为 true 可限制密钥只在相同请求可重用,默认不限制一直可重用,细粒度控制权限需要设为 true
// });
// };
// xhr.send(JSON.stringify(options.Scope));


// // 格式三、(不推荐,分片上传权限不好控制)前端每次请求前都需要通过 getAuthorization 获取签名,后端使用固定密钥或临时密钥计算签名返回给前端
// // 服务端获取签名,请参考对应语言的 COS SDK:https://cloud.tencent.com/document/product/436/6474
// // 注意:这种有安全风险,后端需要通过 method、pathname 严格控制好权限,比如不允许 put / 等
// var method = (options.Method || 'get').toLowerCase();
// var query = options.Query || {};
// var headers = options.Headers || {};
// var pathname = options.Pathname || '/';
// // var url = 'http://127.0.0.1:3000/auth';
// var url = '../server/auth.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.onload = function (e) {
// try {
// var data = JSON.parse(e.target.responseText);
// } catch (e) {
// }
// callback({
// Authorization: data.authorization,
// // XCosSecurityToken: data.sessionToken, // 如果使用临时密钥,需要把 sessionToken 传给 XCosSecurityToken
// });
// };
// xhr.send(JSON.stringify(data));


// // 格式四、(不推荐,适用于前端调试,避免泄露密钥)前端使用固定密钥计算签名,通过 COS.getAuthorization 静态方法计算
// var authorization = COS.getAuthorization({
// SecretId: 'AKIDxxx', // 可传固定密钥或者临时密钥
// SecretKey: 'xxx', // 可传固定密钥或者临时密钥
// Method: options.Method,
// Pathname: options.Pathname,
// Query: options.Query,
// Headers: options.Headers,
// Expires: 900,
// });
// callback({
// Authorization: authorization,
// // XCosSecurityToken: credentials.sessionToken, // 如果使用临时密钥,需要传 XCosSecurityToken
// });


// // 格式五、(不推荐,适用于前端调试,避免泄露密钥)前端使用固定密钥计算签名,通过 cos-auth.js 计算
// var auth = CosAuth({
// Version: '4.0',
// SecretId: 'xxx',
Expand All @@ -88,6 +141,7 @@ var getAuthorization = function (options, callback) {
// Pathname: '/' + (options.Headers && options.Headers['x-cos-copy-source'] ? '' : options.Key),
// });
// callback({Authorization: auth});

};
var dataURItoUploadBody = function (dataURI) {
var byteString = atob(dataURI.split(',')[1]);
Expand Down

0 comments on commit e30d407

Please sign in to comment.