Skip to content

Commit

Permalink
修改 region
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Sep 6, 2017
1 parent ca48fea commit 9979420
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 76 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var cos = new COS({
// 分片上传
cos.sliceUploadFile({
Bucket: 'test',
Region: 'cn-south',
Region: 'ap-guangzhou',
Key: '1.zip',
Body: file
}, function (err, data) {
Expand Down
2 changes: 1 addition & 1 deletion demo/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var config = {
AppId: '1250000000',
Bucket: 'test',
Region: 'cn-south'
Region: 'ap-guangzhou'
};
68 changes: 34 additions & 34 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var getAuthorization = function (options, callback) {
// 方法一(推荐)
var method = (options.method || 'get').toLowerCase();
var pathname = options.pathname || '/';
var url = '../server/auth.php?method=' + method + '&pathname=' + pathname;
var url = '../server/auth.php?method=' + method + '&pathname=' + encodeURIComponent(pathname);
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function (e) {
Expand All @@ -27,7 +27,7 @@ var getAuthorization = function (options, callback) {

// // 方法二(适用于前端调试)
// var authorization = COS.getAuthorization({
// SecretId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// SecretId: 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// method: (options.method || 'get').toLowerCase(),
// pathname: options.pathname || '/',
Expand Down Expand Up @@ -113,23 +113,23 @@ function putBucketAcl() {
cos.putBucketAcl({
Bucket: config.Bucket,
Region: config.Region,
// GrantFullControl: 'uin="1001", uin="1002"',
// GrantWrite: 'uin="1001", uin="1002"',
// GrantRead: 'uin="1001", uin="1002"',
// GrantFullControl: 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"',
// GrantWrite: 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"',
// GrantRead: 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"',
// ACL: 'public-read-write',
// ACL: 'public-read',
// ACL: 'private',
AccessControlPolicy: {
"Owner": {
"ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
},
"Grants": [{
"Grantee": {
"ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
},
"Permission": "READ"
}]
}
ACL: 'private',
// AccessControlPolicy: {
// "Owner": { // AccessControlPolicy 里必须有 owner
// "ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
// },
// "Grants": [{
// "Grantee": {
// "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
// },
// "Permission": "READ"
// }]
// }
}, function (err, data) {
console.log(err || data);
});
Expand Down Expand Up @@ -251,7 +251,7 @@ function putBucketPolicy() {
"name/cos:AbortMultipartUpload",
"name/cos:AppendObject"
],
// "resource": ["qcs::cos:cn-south:uid/1250000000:test-1250000000.cn-south.myqcloud.com//1250000000/test/*"] // 1250000000 是 appid
// "resource": ["qcs::cos:ap-guangzhou:uid/1250000000:test-1250000000.cos.ap-guangzhou.myqcloud.com//1250000000/test/*"] // 1250000000 是 appid
"resource": ["qcs::cos:" + config.Region + ":uid/" + AppId + ":" + Bucket + "-" + AppId + ".cos." + config.Region + ".myqcloud.com//" + AppId + "/" + Bucket + "/*"] // 1250000000 是 appid
}
]
Expand Down Expand Up @@ -333,7 +333,7 @@ function deleteBucket() {
function putObject() {
// 创建测试文件
var filename = '1mb.zip';
var blob = util.createFile({size: 1024 * 1024});
var blob = util.createFile({size: 10});
// 调用方法
cos.putObject({
Bucket: config.Bucket, /* 必须 */
Expand Down Expand Up @@ -394,23 +394,23 @@ function putObjectAcl() {
Bucket: config.Bucket,
Region: config.Region,
Key: '1mb.zip',
// GrantFullControl: 'uin="1001", uin="1002"',
// GrantWrite: 'uin="1001", uin="1002"',
// GrantRead: 'uin="1001", uin="1002"',
// GrantFullControl: 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"',
// GrantWrite: 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"',
// GrantRead: 'id="qcs::cam::uin/1001:uin/1001", id="qcs::cam::uin/1002:uin/1002"',
// ACL: 'public-read-write',
// ACL: 'public-read',
// ACL: 'private',
AccessControlPolicy: {
"Owner": {
"ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
},
"Grants": [{
"Grantee": {
"ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
},
"Permission": "READ"
}]
}
ACL: 'private',
// AccessControlPolicy: {
// "Owner": { // AccessControlPolicy 里必须有 owner
// "ID": 'qcs::cam::uin/10001:uin/10001' // 10001 是 QQ 号
// },
// "Grants": [{
// "Grantee": {
// "ID": "qcs::cam::uin/10002:uin/10002", // 10002 是 QQ 号
// },
// "Permission": "READ"
// }]
// }
}, function (err, data) {
console.log(err || data);
});
Expand Down
29 changes: 14 additions & 15 deletions dist/cos-js-sdk-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -3022,6 +3022,7 @@ var getAuth = function (opt) {
var pathname = opt.pathname || '/';
var queryParams = opt.params || '';
var headers = opt.headers || '';
pathname.indexOf('/') === -1 && (pathname = '/' + pathname);

if (!SecretId) return console.error('lack of param SecretId');
if (!SecretKey) return console.error('lack of param SecretKey');
Expand All @@ -3039,13 +3040,12 @@ var getAuth = function (opt) {
var obj2str = function (obj) {
var i, key, val;
var list = [];
var keyList = Object.keys(obj);
var keyList = getObjectKeys(obj);
for (i = 0; i < keyList.length; i++) {
key = keyList[i];
val = obj[key] || '';
key = key.toLowerCase();
key = camSafeUrlEncode(key);
list.push(key + '=' + camSafeUrlEncode(val));
list.push(camSafeUrlEncode(key) + '=' + camSafeUrlEncode(val));
}
return list.join('&');
};
Expand Down Expand Up @@ -3238,10 +3238,10 @@ var checkParams = function (apiName, params) {

var apiWrapper = function (apiName, apiFn) {
var regionMap = {
'gz': 'cn-south',
'tj': 'cn-north',
'sh': 'cn-east',
'cd': 'cn-southwest'
'gz': 'ap-guangzhou',
'tj': 'ap-beijing-2',
'sh': 'ap-shanghai',
'cd': 'ap-chengdu'
};
return function (params, callback) {
callback = callback || function () {};
Expand Down Expand Up @@ -16174,7 +16174,6 @@ function _putObject(params, callback) {
headers['Content-Type'] = params['ContentType'];
headers['Expect'] = params['Expect'];
headers['Expires'] = params['Expires'];
// headers['x-cos-content-sha1'] = params['ContentSha1'];
headers['x-cos-acl'] = params['ACL'];
headers['x-cos-grant-read'] = params['GrantRead'];
headers['x-cos-grant-write'] = params['GrantWrite'];
Expand Down Expand Up @@ -16503,7 +16502,6 @@ function putObjectCopy(params, callback) {
headers['Content-Type'] = params['ContentType'];
headers['Expect'] = params['Expect'];
headers['Expires'] = params['Expires'];
// headers['x-cos-content-sha1'] = params['ContentSha1'];

for (var key in params) {
if (key.indexOf('x-cos-meta-') > -1) {
Expand Down Expand Up @@ -16669,7 +16667,6 @@ function multipartUpload(params, callback) {

headers['Content-Length'] = params['ContentLength'];
headers['Expect'] = params['Expect'];
// headers['x-cos-content-sha1'] = params['ContentSha1'];

var PartNumber = params['PartNumber'];
var UploadId = params['UploadId'];
Expand Down Expand Up @@ -16943,7 +16940,11 @@ function getUrl(params) {
var appId = params.appId;
var protocol = util.isBrowser && location.protocol === 'https:' ? 'https:' : 'http:';
if (!domain) {
domain = '{{Bucket}}-{{AppId}}.cos.{{Region}}.myqcloud.com';
if (['cn-south', 'cn-south-2', 'cn-north', 'cn-east', 'cn-southwest', 'sg'].indexOf(region) > -1) {
domain = '{{Bucket}}-{{AppId}}.{{Region}}.myqcloud.com';
} else {
domain = '{{Bucket}}-{{AppId}}.cos.{{Region}}.myqcloud.com';
}
}
domain = domain.replace(/\{\{AppId\}\}/ig, appId).replace(/\{\{Bucket\}\}/ig, bucket).replace(/\{\{Region\}\}/ig, region).replace(/\{\{.*?\}\}/ig, '');
if (!/^[a-zA-Z]+:\/\//.test(domain)) {
Expand All @@ -16955,7 +16956,7 @@ function getUrl(params) {
var url = domain;

if (object) {
url += '/' + encodeURIComponent(object);
url += '/' + encodeURIComponent(object).replace(/%2F/g, '/');
}

if (action) {
Expand Down Expand Up @@ -29176,7 +29177,7 @@ function plural(ms, n, name) {

module.exports = {
"name": "cos-js-sdk-v5",
"version": "0.0.8",
"version": "0.0.9",
"description": "cos js sdk v5",
"main": "index.js",
"scripts": {
Expand All @@ -29187,8 +29188,6 @@ module.exports = {
"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"
Expand Down
6 changes: 3 additions & 3 deletions dist/cos-js-sdk-v5.min.js

Large diffs are not rendered by default.

4 changes: 2 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.0.8",
"version": "0.0.9",
"description": "cos js sdk v5",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -28,6 +28,6 @@
"devDependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"webpack": "^3.2.0"
"express": "^4.15.4"
}
}
55 changes: 35 additions & 20 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ var util = {

var getAuthorization = function (options, callback) {

// // 方法一(推荐)
// var method = (options.method || 'get').toLowerCase();
// var pathname = options.pathname || '/';
// var url = '../server/auth.php?method=' + method + '&pathname=' + pathname;
// var xhr = new XMLHttpRequest();
// xhr.open('GET', url, true);
// xhr.onload = function (e) {
// callback(e.target.responseText);
// };
// xhr.send();
// 方法一(推荐)
var method = (options.method || 'get').toLowerCase();
var pathname = options.pathname || '/';
var url = '../server/auth.php?method=' + method + '&pathname=' + pathname;
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function (e) {
callback(e.target.responseText);
};
xhr.send();

// 方法二(适用于前端调试)
var authorization = COS.getAuthorization({
SecretId: 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
method: (options.method || 'get').toLowerCase(),
pathname: options.pathname || '/',
});
callback(authorization);
// // 方法二(适用于前端调试)
// var authorization = COS.getAuthorization({
// SecretId: 'AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// SecretKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// method: (options.method || 'get').toLowerCase(),
// pathname: options.pathname || '/',
// });
// callback(authorization);

};

Expand Down Expand Up @@ -431,6 +431,21 @@ QUnit.test('putObject()', function (assert) {
});
});

QUnit.test('Key 特殊字符', function (assert) {
return new Promise(function (done) {
cos.putObject({
Bucket: config.Bucket,
Region: config.Region,
Key: '(!\'*) "#$%&+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~',
Body: Date.now().toString()
}, function (err, data) {
if (err) throw err;
assert.ok(data, 'putObject 特殊字符的 Key 能通过');
done();
});
});
});

QUnit.test('getObject()', function (assert) {
new Promise(function (done) {
var key = '1.txt';
Expand Down Expand Up @@ -1429,7 +1444,7 @@ QUnit.test('params check', function (assert) {
Bucket: config.Bucket,
Region: 'gz'
}, function (err, data) {
assert.ok(err.error === 'Region should be cn-south');
assert.ok(err.error === 'Region should be ap-guangzhou');
done();
});
});
Expand All @@ -1439,7 +1454,7 @@ QUnit.test('params check', function (assert) {
return new Promise(function (done) {
cos.headBucket({
Bucket: config.Bucket,
Region: 'cos.cn-south'
Region: 'cos.ap-guangzhou'
}, function (err, data) {
assert.ok(err.error === 'Region should not be start with "cos."');
done();
Expand Down

0 comments on commit 9979420

Please sign in to comment.