Skip to content

Commit

Permalink
修复报错,修改签名参数
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Sep 28, 2017
1 parent 3e881aa commit ccf9017
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 50 deletions.
14 changes: 8 additions & 6 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ var util = {
var getAuthorization = function (options, callback) {

// 方法一(推荐)
var method = (options.method || 'get').toLowerCase();
var pathname = options.pathname || '/'
var method = (options.Method || 'get').toLowerCase();
var key = options.Key || '';
var pathname = key.indexOf('/') === 0 ? key : '/' + key;

var url = '../server/auth.php?method=' + method + '&pathname=' + encodeURIComponent(pathname);
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
Expand All @@ -29,8 +31,8 @@ var getAuthorization = function (options, callback) {
// var authorization = COS.getAuthorization({
// SecretId: 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// method: (options.method || 'get').toLowerCase(),
// pathname: options.pathname || '/',
// Method: options.Method,
// Key: options.Key,
// });
// callback(authorization);

Expand Down Expand Up @@ -84,8 +86,8 @@ function getAuth() {
}
var key = '1.png';
getAuthorization({
method: 'get',
pathname: '/' + key
Method: 'get',
Key: key
}, function (auth) {
console.log('http://' + Bucket + '-' + AppId + '.cos.' + config.Region + '.myqcloud.com' + '/' + key + '?sign=' + encodeURIComponent(auth));
});
Expand Down
25 changes: 11 additions & 14 deletions dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -2891,8 +2891,8 @@ var getAuth = function (opt) {

var SecretId = opt.SecretId;
var SecretKey = opt.SecretKey;
var method = (opt.method || 'get').toLowerCase();
var pathname = opt.pathname || '/';
var method = (opt.method || opt.Method || 'get').toLowerCase();
var pathname = opt.pathname || opt.Key || '/';
var queryParams = opt.params || '';
var headers = opt.headers || '';
pathname.indexOf('/') === -1 && (pathname = '/' + pathname);
Expand Down Expand Up @@ -13319,8 +13319,8 @@ function multipartAbort(params, callback) {
*/
function getAuth(params) {
return util.getAuth({
method: params.Method || 'get',
pathname: '/' + (params.Key + ''),
Method: params.Method || 'get',
Key: params.Key || '',
SecretId: params.SecretId || this.options.SecretId || '',
SecretKey: params.SecretKey || this.options.SecretKey || ''
});
Expand Down Expand Up @@ -13432,10 +13432,6 @@ function submitRequest(params, callback) {
json: json
};

if (object) {
object = '/' + object;
}

var innerSender;
var outerSender = {
abort: function () {
Expand Down Expand Up @@ -13536,13 +13532,15 @@ function submitRequest(params, callback) {
// 获取签名
if (this.options.getAuthorization) {
this.options.getAuthorization({
Method: opt.method,
Key: object || '',
method: opt.method,
pathname: object || '/'
pathname: '/' + (object || '')
}, getAuthorizationCallback);
} else {
var auth = util.getAuth({
method: opt.method,
pathname: object || '/',
Method: opt.method,
Key: object || '',
SecretId: params.SecretId || this.options.SecretId,
SecretKey: params.SecretKey || this.options.SecretKey
});
Expand Down Expand Up @@ -17869,8 +17867,7 @@ var request = function (options, callback) {

// qs
if (options.qs) {
// var qsStr = queryString.stringify(options.qs);
var qsStr = 1;
var qsStr = queryString.stringify(options.qs);
if (qsStr) {
options.url += (options.url.indexOf('?') === -1 ? '?' : '&') + qsStr;
}
Expand Down Expand Up @@ -25462,7 +25459,7 @@ function plural(ms, n, name) {
/* 79 */
/***/ (function(module, exports) {

module.exports = {"name":"cos-js-sdk-v5","version":"0.1.1","description":"cos js sdk v5","main":"index.js","scripts":{"dev":"SET NODE_ENV=development&& webpack -w","build":"SET NODE_ENV=production&& webpack"},"repository":{"type":"git","url":"git+https://github.com/tencentyun/cos-js-sdk-v5.git"},"keywords":[],"author":"carsonxu","license":"ISC","bugs":{"url":"https://github.com/tencentyun/cos-js-sdk-v5/issues"},"homepage":"https://github.com/tencentyun/cos-js-sdk-v5#readme","dependencies":{"async":"^2.2.0","eventproxy":"^0.3.5","json-loader":"^0.5.7","lodash":"^4.17.4","querystring":"^0.2.0","request":"^2.81.0","xml2js":"^0.4.17"},"devDependencies":{"babel-core":"^6.25.0","babel-loader":"^7.1.1","express":"^4.15.4"}}
module.exports = {"name":"cos-js-sdk-v5","version":"0.1.2","description":"cos js sdk v5","main":"index.js","scripts":{"dev":"SET NODE_ENV=development&& webpack -w","build":"SET NODE_ENV=production&& webpack"},"repository":{"type":"git","url":"git+https://github.com/tencentyun/cos-js-sdk-v5.git"},"keywords":[],"author":"carsonxu","license":"ISC","bugs":{"url":"https://github.com/tencentyun/cos-js-sdk-v5/issues"},"homepage":"https://github.com/tencentyun/cos-js-sdk-v5#readme","dependencies":{"async":"^2.2.0","eventproxy":"^0.3.5","json-loader":"^0.5.7","lodash":"^4.17.4","querystring":"^0.2.0","request":"^2.81.0","xml2js":"^0.4.17"},"devDependencies":{"babel-core":"^6.25.0","babel-loader":"^7.1.1","express":"^4.15.4","webpack":"^3.6.0"}}

/***/ })
/******/ ]);
6 changes: 3 additions & 3 deletions dist/cos-js-sdk-v5.min.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4116,8 +4116,7 @@ var request = function (options, callback) {

// qs
if (options.qs) {
// var qsStr = queryString.stringify(options.qs);
var qsStr = 1;
var qsStr = queryString.stringify(options.qs);
if (qsStr) {
options.url += (options.url.indexOf('?') === -1 ? '?' : '&') + qsStr;
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cos-js-sdk-v5",
"version": "0.1.1",
"version": "0.1.2",
"description": "cos js sdk v5",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -30,6 +30,7 @@
"devDependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"express": "^4.15.4"
"express": "^4.15.4",
"webpack": "^3.6.0"
}
}
16 changes: 7 additions & 9 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1541,8 +1541,8 @@ function multipartAbort(params, callback) {
*/
function getAuth(params) {
return util.getAuth({
method: params.Method || 'get',
pathname: '/' + (params.Key + ''),
Method: params.Method || 'get',
Key: params.Key || '',
SecretId: params.SecretId || this.options.SecretId || '',
SecretKey: params.SecretKey || this.options.SecretKey || ''
});
Expand Down Expand Up @@ -1658,10 +1658,6 @@ function submitRequest(params, callback) {
json: json,
};

if (object) {
object = '/' + object;
}

var innerSender;
var outerSender = {
abort: function () {
Expand Down Expand Up @@ -1760,13 +1756,15 @@ function submitRequest(params, callback) {
// 获取签名
if (this.options.getAuthorization) {
this.options.getAuthorization({
Method: opt.method,
Key: object || '',
method: opt.method,
pathname: object || '/'
pathname: '/' + (object || '')
}, getAuthorizationCallback);
} else {
var auth = util.getAuth({
method: opt.method,
pathname: object || '/',
Method: opt.method,
Key: object || '',
SecretId: params.SecretId || this.options.SecretId,
SecretKey: params.SecretKey || this.options.SecretKey,
});
Expand Down
4 changes: 2 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ var getAuth = function (opt) {

var SecretId = opt.SecretId;
var SecretKey = opt.SecretKey;
var method = (opt.method || 'get').toLowerCase();
var pathname = opt.pathname || '/';
var method = (opt.method || opt.Method || 'get').toLowerCase();
var pathname = opt.pathname || opt.Key || '/';
var queryParams = opt.params || '';
var headers = opt.headers || '';
pathname.indexOf('/') === -1 && (pathname = '/' + pathname);
Expand Down
13 changes: 7 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ var util = {
var getAuthorization = function (options, callback) {

// 方法一(推荐)
var method = (options.method || 'get').toLowerCase();
var pathname = options.pathname || '/';
var method = (options.Method || 'get').toLowerCase();
var key = options.Key || '';
var pathname = key.indexOf('/') === 0 ? key : '/' + key;
var url = '../server/auth.php?method=' + method + '&pathname=' + encodeURIComponent(pathname);
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
Expand All @@ -39,8 +40,8 @@ var getAuthorization = function (options, callback) {
// var authorization = COS.getAuthorization({
// SecretId: 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// method: (options.method || 'get').toLowerCase(),
// pathname: options.pathname || '/',
// Method: options.Method,
// Key: options.Key,
// });
// callback(authorization);

Expand Down Expand Up @@ -88,8 +89,8 @@ QUnit.test('getAuth()', function (assert) {
Body: content
}, function (err, data) {
getAuthorization({
method: 'get',
pathname: '/' + key
Method: 'get',
Key: key
}, function (auth) {
var link = 'http://' + Bucket + '-' + AppId + '.cos.' + config.Region + '.myqcloud.com/' + key +
'?sign=' + encodeURIComponent(auth);
Expand Down
7 changes: 1 addition & 6 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ module.exports = {
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/
},
// {
// test: /\.json$/,
// use: 'json-loader',
// // loader: 'json-loader',
// }
}
]
},
devServer: {
Expand Down

0 comments on commit ccf9017

Please sign in to comment.