Skip to content

Commit

Permalink
fix:getObjectUrl报错问题
Browse files Browse the repository at this point in the history
  • Loading branch information
livehigh committed Oct 27, 2021
1 parent 2ee9039 commit c516a9b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 91 deletions.
61 changes: 31 additions & 30 deletions demo-album/lib/cos-wx-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,34 @@ function camSafeUrlEncode(str) {
.replace(/\*/g, '%2A');
}

function getObjectKeys(obj, forKey) {
var list = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
list.push(forKey ? camSafeUrlEncode(key).toLowerCase() : key);
}
}
return list.sort(function (a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a === b ? 0 : (a > b ? 1 : -1);
});
};

var obj2str = function (obj) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = (obj[key] === undefined || obj[key] === null) ? '' : ('' + obj[key]);
key = camSafeUrlEncode(key).toLowerCase();
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val)
}
return list.join('&');
};

//测试用的key后面可以去掉
var getAuth = function (opt) {
opt = opt || {};
Expand All @@ -120,34 +148,6 @@ var getAuth = function (opt) {
if (!SecretId) return console.error('missing param SecretId');
if (!SecretKey) return console.error('missing param SecretKey');

var getObjectKeys = function (obj, forKey) {
var list = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
list.push(forKey ? camSafeUrlEncode(key).toLowerCase() : key);
}
}
return list.sort(function (a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a === b ? 0 : (a > b ? 1 : -1);
});
};

var obj2str = function (obj) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = (obj[key] === undefined || obj[key] === null) ? '' : ('' + obj[key]);
key = camSafeUrlEncode(key).toLowerCase();
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val)
}
return list.join('&');
};

// 签名有效起止时间
var now = Math.round(getSkewTime(opt.SystemClockOffset) / 1000) - 1;
var exp = now;
Expand All @@ -172,7 +172,7 @@ var getAuth = function (opt) {
var signKey = CryptoJS.HmacSHA1(qKeyTime, SecretKey).toString();

// 步骤二:构成 FormatString
var formatString = [method, pathname, obj2str(queryParams), obj2str(headers), ''].join('\n');
var formatString = [method, pathname, util.obj2str(queryParams), util.obj2str(headers), ''].join('\n');

// 步骤三:计算 StringToSign
var stringToSign = ['sha1', qSignTime, CryptoJS.SHA1(formatString).toString(), ''].join('\n');
Expand Down Expand Up @@ -655,6 +655,7 @@ var util = {
throttleOnProgress: throttleOnProgress,
getFileSize: getFileSize,
getSkewTime: getSkewTime,
obj2str: obj2str,
getAuth: getAuth,
compareVersion: compareVersion,
canFileSlice: canFileSlice,
Expand Down Expand Up @@ -2338,7 +2339,7 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '1.0.10';
COS.version = '1.0.11';

module.exports = COS;

Expand Down
61 changes: 31 additions & 30 deletions demo/lib/cos-wx-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,34 @@ function camSafeUrlEncode(str) {
.replace(/\*/g, '%2A');
}

function getObjectKeys(obj, forKey) {
var list = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
list.push(forKey ? camSafeUrlEncode(key).toLowerCase() : key);
}
}
return list.sort(function (a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a === b ? 0 : (a > b ? 1 : -1);
});
};

var obj2str = function (obj) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = (obj[key] === undefined || obj[key] === null) ? '' : ('' + obj[key]);
key = camSafeUrlEncode(key).toLowerCase();
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val)
}
return list.join('&');
};

//测试用的key后面可以去掉
var getAuth = function (opt) {
opt = opt || {};
Expand All @@ -120,34 +148,6 @@ var getAuth = function (opt) {
if (!SecretId) return console.error('missing param SecretId');
if (!SecretKey) return console.error('missing param SecretKey');

var getObjectKeys = function (obj, forKey) {
var list = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
list.push(forKey ? camSafeUrlEncode(key).toLowerCase() : key);
}
}
return list.sort(function (a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a === b ? 0 : (a > b ? 1 : -1);
});
};

var obj2str = function (obj) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = (obj[key] === undefined || obj[key] === null) ? '' : ('' + obj[key]);
key = camSafeUrlEncode(key).toLowerCase();
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val)
}
return list.join('&');
};

// 签名有效起止时间
var now = Math.round(getSkewTime(opt.SystemClockOffset) / 1000) - 1;
var exp = now;
Expand All @@ -172,7 +172,7 @@ var getAuth = function (opt) {
var signKey = CryptoJS.HmacSHA1(qKeyTime, SecretKey).toString();

// 步骤二:构成 FormatString
var formatString = [method, pathname, obj2str(queryParams), obj2str(headers), ''].join('\n');
var formatString = [method, pathname, util.obj2str(queryParams), util.obj2str(headers), ''].join('\n');

// 步骤三:计算 StringToSign
var stringToSign = ['sha1', qSignTime, CryptoJS.SHA1(formatString).toString(), ''].join('\n');
Expand Down Expand Up @@ -655,6 +655,7 @@ var util = {
throttleOnProgress: throttleOnProgress,
getFileSize: getFileSize,
getSkewTime: getSkewTime,
obj2str: obj2str,
getAuth: getAuth,
compareVersion: compareVersion,
canFileSlice: canFileSlice,
Expand Down Expand Up @@ -2338,7 +2339,7 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '1.0.10';
COS.version = '1.0.11';

module.exports = COS;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cos-wx-sdk-v5",
"version": "1.0.10",
"version": "1.0.11",
"description": "小程序 SDK for [腾讯云对象存储服务](https://cloud.tencent.com/product/cos)",
"main": "demo/lib/cos-wx-sdk-v5.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/cos.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ base.init(COS, task);
advance.init(COS, task);

COS.getAuthorization = util.getAuth;
COS.version = '1.0.10';
COS.version = '1.0.11';

module.exports = COS;
59 changes: 30 additions & 29 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@ function camSafeUrlEncode(str) {
.replace(/\*/g, '%2A');
}

function getObjectKeys(obj, forKey) {
var list = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
list.push(forKey ? camSafeUrlEncode(key).toLowerCase() : key);
}
}
return list.sort(function (a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a === b ? 0 : (a > b ? 1 : -1);
});
};

var obj2str = function (obj) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = (obj[key] === undefined || obj[key] === null) ? '' : ('' + obj[key]);
key = camSafeUrlEncode(key).toLowerCase();
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val)
}
return list.join('&');
};

//测试用的key后面可以去掉
var getAuth = function (opt) {
opt = opt || {};
Expand All @@ -40,34 +68,6 @@ var getAuth = function (opt) {
if (!SecretId) return console.error('missing param SecretId');
if (!SecretKey) return console.error('missing param SecretKey');

var getObjectKeys = function (obj, forKey) {
var list = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
list.push(forKey ? camSafeUrlEncode(key).toLowerCase() : key);
}
}
return list.sort(function (a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
return a === b ? 0 : (a > b ? 1 : -1);
});
};

var obj2str = function (obj) {
var i, key, val;
var list = [];
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = (obj[key] === undefined || obj[key] === null) ? '' : ('' + obj[key]);
key = camSafeUrlEncode(key).toLowerCase();
val = camSafeUrlEncode(val) || '';
list.push(key + '=' + val)
}
return list.join('&');
};

// 签名有效起止时间
var now = Math.round(getSkewTime(opt.SystemClockOffset) / 1000) - 1;
var exp = now;
Expand All @@ -92,7 +92,7 @@ var getAuth = function (opt) {
var signKey = CryptoJS.HmacSHA1(qKeyTime, SecretKey).toString();

// 步骤二:构成 FormatString
var formatString = [method, pathname, obj2str(queryParams), obj2str(headers), ''].join('\n');
var formatString = [method, pathname, util.obj2str(queryParams), util.obj2str(headers), ''].join('\n');

// 步骤三:计算 StringToSign
var stringToSign = ['sha1', qSignTime, CryptoJS.SHA1(formatString).toString(), ''].join('\n');
Expand Down Expand Up @@ -575,6 +575,7 @@ var util = {
throttleOnProgress: throttleOnProgress,
getFileSize: getFileSize,
getSkewTime: getSkewTime,
obj2str: obj2str,
getAuth: getAuth,
compareVersion: compareVersion,
canFileSlice: canFileSlice,
Expand Down

0 comments on commit c516a9b

Please sign in to comment.