Skip to content

Commit 4bce53b

Browse files
committed
Key 拼接 Url 使用 camSafeUrlEncode
1 parent b631f1b commit 4bce53b

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

demo/demo.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ var cos = new COS({
1818
});
1919
var TaskId;
2020

21+
function camSafeUrlEncode(str) {
22+
return encodeURIComponent(str)
23+
.replace(/!/g, '%21')
24+
.replace(/'/g, '%27')
25+
.replace(/\(/g, '%28')
26+
.replace(/\)/g, '%29')
27+
.replace(/\*/g, '%2A');
28+
}
29+
2130
function getService() {
2231
cos.getService(function (err, data) {
2332
console.log(err || data);
@@ -32,7 +41,7 @@ function getAuth() {
3241
Expires: 60,
3342
});
3443
// 注意:这里的 Bucket 格式是 test-1250000000
35-
console.log('http://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com' + '/' + encodeURIComponent(key).replace(/%2F/g, '/') + '?sign=' + encodeURIComponent(auth));
44+
console.log('http://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com' + '/' + camSafeUrlEncode(key).replace(/%2F/g, '/') + '?sign=' + encodeURIComponent(auth));
3645
}
3746

3847
function getV4Auth() {
@@ -44,7 +53,7 @@ function getV4Auth() {
4453
Expires: 60,
4554
});
4655
// 注意:这里的 Bucket 格式是 test-1250000000
47-
console.log('http://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com' + '/' + encodeURIComponent(key).replace(/%2F/g, '/') + '?sign=' + encodeURIComponent(auth));
56+
console.log('http://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com' + '/' + camSafeUrlEncode(key).replace(/%2F/g, '/') + '?sign=' + encodeURIComponent(auth));
4857
}
4958

5059
function getObjectUrl() {
@@ -473,7 +482,7 @@ function putObjectCopy() {
473482
Bucket: config.Bucket, // Bucket 格式:test-1250000000
474483
Region: config.Region,
475484
Key: '1mb.copy.zip',
476-
CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + encodeURIComponent('1mb.zip').replace(/%2F/g, '/'),
485+
CopySource: config.Bucket + '.cos.' + config.Region + '.myqcloud.com/' + camSafeUrlEncode('1mb.zip').replace(/%2F/g, '/'),
477486
}, function (err, data) {
478487
console.log(err || data);
479488
});
@@ -706,7 +715,7 @@ function sliceCopyFile() {
706715
var sourceName = '3mb.zip';
707716
var Key = '3mb.copy.zip';
708717

709-
var sourcePath = config.Bucket + '.cos.' + config.Region + '.myqcloud.com/'+ encodeURIComponent(sourceName).replace(/%2F/g, '/');
718+
var sourcePath = config.Bucket + '.cos.' + config.Region + '.myqcloud.com/'+ camSafeUrlEncode(sourceName).replace(/%2F/g, '/');
710719

711720
cos.sliceCopyFile({
712721
Bucket: config.Bucket, // Bucket 格式:test-1250000000

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cos-nodejs-sdk-v5",
3-
"version": "2.5.0",
3+
"version": "2.5.1",
44
"description": "cos nodejs sdk v5",
55
"main": "index.js",
66
"scripts": {

sdk/base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2086,7 +2086,7 @@ function getUrl(params) {
20862086
}
20872087
url += '/';
20882088
if (object) {
2089-
url += encodeURIComponent(object).replace(/%2F/g, '/');
2089+
url += util.camSafeUrlEncode(object).replace(/%2F/g, '/');
20902090
}
20912091

20922092
if (params.isLocation) {

sdk/util.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ var util = {
532532
filter: filter,
533533
clone: clone,
534534
uuid: uuid,
535+
camSafeUrlEncode: camSafeUrlEncode,
535536
throttleOnProgress: throttleOnProgress,
536537
getFileSize: getFileSize,
537538
isBrowser: false,

test/test.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ if (process.env.AppId) {
1414
Bucket: process.env.Bucket,
1515
Region: process.env.Region
1616
}
17-
};
17+
}
1818
var dataURItoUploadBody = function (dataURI) {
1919
return Buffer.from(dataURI.split(',')[1], 'base64');
2020
};
@@ -25,7 +25,14 @@ var createFileSync = function (filePath, size) {
2525
}
2626
return filePath;
2727
};
28-
28+
function camSafeUrlEncode(str) {
29+
return encodeURIComponent(str)
30+
.replace(/!/g, '%21')
31+
.replace(/'/g, '%27')
32+
.replace(/\(/g, '%28')
33+
.replace(/\)/g, '%29')
34+
.replace(/\*/g, '%2A');
35+
}
2936
var assert = require("assert");
3037
assert.ok = assert;
3138
var test = function (name, fn) {
@@ -39,7 +46,7 @@ var group = function (name, fn) {
3946
fn.apply(this, arguments);
4047
});
4148
};
42-
var proxy = '';
49+
var proxy = 'http://web-proxy.tencent.com:8080';
4350

4451
var cos = new COS({
4552
SecretId: config.SecretId,
@@ -119,7 +126,6 @@ group('getService()', function () {
119126
test('能正常列出 Bucket', function (done, assert) {
120127
prepareBucket().then(function () {
121128
cos.getService(function (err, data) {
122-
console.log(err, data);
123129
var hasBucket = false;
124130
data.Buckets && data.Buckets.forEach(function (item) {
125131
if (item.Name === BucketLongName && (item.Location === config.Region || !item.Location)) {
@@ -178,7 +184,7 @@ group('getAuth()', function () {
178184
AuthData = {Authorization: AuthData};
179185
}
180186
var link = 'http://' + config.Bucket + '.cos.' + config.Region + '.myqcloud.com' + '/' +
181-
encodeURIComponent(key).replace(/%2F/g, '/') + '?' + AuthData.Authorization +
187+
camSafeUrlEncode(key).replace(/%2F/g, '/') + '?' + AuthData.Authorization +
182188
(AuthData.XCosSecurityToken ? '&x-cos-security-token=' + AuthData.XCosSecurityToken : '');
183189
request({
184190
url: link,

0 commit comments

Comments
 (0)