Skip to content

Commit 5188200

Browse files
authored
fix batch rs host (#423)
1 parent 802744b commit 5188200

File tree

3 files changed

+111
-10
lines changed

3 files changed

+111
-10
lines changed

qiniu/storage/rs.js

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -550,21 +550,51 @@ function listPrefixReqV2 (mac, config, bucket, options, callbackFunc) {
550550

551551
// 批量文件管理请求,支持stat,chgm,chtype,delete,copy,move
552552
BucketManager.prototype.batch = function (operations, callbackFunc) {
553-
var scheme = this.config.useHttpsDomain ? 'https://' : 'http://';
554-
var requestURI = scheme + conf.RS_HOST + '/batch';
555-
var reqParams = {
553+
if (!operations.length) {
554+
callbackFunc(new Error('Empty operations'), null, null)
555+
}
556+
557+
let bucket;
558+
for (const op of operations) {
559+
const [, , entry] = op.split('/');
560+
if (!entry) {
561+
continue;
562+
}
563+
[bucket] = util.decodedEntry(entry);
564+
if (bucket) {
565+
break;
566+
}
567+
}
568+
if (!bucket) {
569+
callbackFunc(new Error('Empty bucket'));
570+
return;
571+
}
572+
573+
util.prepareZone(this, this.mac.accessKey, bucket, function (err, ctx) {
574+
if (err) {
575+
callbackFunc(err, null, null);
576+
return;
577+
}
578+
batchReq(ctx.mac, ctx.config, operations, callbackFunc);
579+
});
580+
};
581+
582+
function batchReq (mac, config, operations, callbackFunc) {
583+
const scheme = config.useHttpsDomain ? 'https://' : 'http://';
584+
const requestURI = scheme + config.zone.rsHost + '/batch';
585+
const reqParams = {
556586
op: operations
557587
};
558-
var reqBody = querystring.stringify(reqParams);
588+
const reqBody = querystring.stringify(reqParams);
559589
rpc.postWithOptions(
560590
requestURI,
561591
reqBody,
562592
{
563-
mac: this.mac
593+
mac: mac
564594
},
565595
callbackFunc
566596
);
567-
};
597+
}
568598

569599
// 批量操作支持的指令构造器
570600
exports.statOp = function (bucket, key) {
@@ -1309,17 +1339,28 @@ BucketManager.prototype.listBucketDomains = function (bucket, callbackFunc) {
13091339

13101340
// 解冻归档存储文件
13111341
BucketManager.prototype.restoreAr = function (entry, freezeAfterDays, callbackFunc) {
1312-
var scheme = this.config.useHttpsDomain ? 'https://' : 'http://';
1313-
var requestURI = scheme + conf.RS_HOST + '/restoreAr/' + util.urlsafeBase64Encode(entry) + '/freezeAfterDays/' + freezeAfterDays;
1342+
const [bucket] = entry.split(':');
1343+
util.prepareZone(this, this.mac.accessKey, bucket, function (err, ctx) {
1344+
if (err) {
1345+
callbackFunc(err, null, null);
1346+
return;
1347+
}
1348+
restoreArReq(ctx.mac, ctx.config, entry, freezeAfterDays, callbackFunc);
1349+
});
1350+
};
1351+
1352+
function restoreArReq (mac, config, entry, freezeAfterDays, callbackFunc) {
1353+
const scheme = config.useHttpsDomain ? 'https://' : 'http://';
1354+
const requestURI = scheme + config.zone.rsHost + '/restoreAr/' + util.urlsafeBase64Encode(entry) + '/freezeAfterDays/' + freezeAfterDays;
13141355
rpc.postWithOptions(
13151356
requestURI,
13161357
null,
13171358
{
1318-
mac: this.mac
1359+
mac: mac
13191360
},
13201361
callbackFunc
13211362
);
1322-
};
1363+
}
13231364

13241365
// 上传策略
13251366
// @link https://developer.qiniu.com/kodo/manual/1206/put-policy

qiniu/util.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ exports.encodedEntry = function (bucket, key) {
4949
return exports.urlsafeBase64Encode(strToEncode);
5050
};
5151

52+
exports.decodedEntry = function (entry) {
53+
const [bucket, key] = exports.urlSafeBase64Decode(entry).split(':');
54+
return [bucket, key];
55+
};
56+
5257
// Get accessKey from uptoken
5358
exports.getAKFromUptoken = function (uploadToken) {
5459
var sepIndex = uploadToken.indexOf(':');

test/util.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,61 @@ describe('test util functions', function () {
370370
should.equal(actual, expect, msg);
371371
}
372372
});
373+
374+
it('test decodedEntry', function () {
375+
const caseList = [
376+
{
377+
msg: 'normal',
378+
expect: {
379+
bucket: 'qiniuphotos',
380+
key: 'gogopher.jpg'
381+
},
382+
entry: 'cWluaXVwaG90b3M6Z29nb3BoZXIuanBn'
383+
},
384+
{
385+
msg: 'key empty',
386+
expect: {
387+
bucket: 'qiniuphotos',
388+
key: ''
389+
},
390+
entry: 'cWluaXVwaG90b3M6'
391+
},
392+
{
393+
msg: 'key undefined',
394+
expect: {
395+
bucket: 'qiniuphotos',
396+
key: undefined
397+
},
398+
entry: 'cWluaXVwaG90b3M='
399+
},
400+
{
401+
msg: 'key need replace plus symbol',
402+
expect: {
403+
bucket: 'qiniuphotos',
404+
key: '012ts>a'
405+
},
406+
entry: 'cWluaXVwaG90b3M6MDEydHM-YQ=='
407+
},
408+
{
409+
msg: 'key need replace slash symbol',
410+
expect: {
411+
bucket: 'qiniuphotos',
412+
key: '012ts?a'
413+
},
414+
entry: 'cWluaXVwaG90b3M6MDEydHM_YQ=='
415+
}
416+
];
417+
418+
for (let i = 0; i < caseList.length; i++) {
419+
const [actualBucket, actualKey] = qiniu.util.decodedEntry(caseList[i].entry);
420+
const expect = caseList[i].expect;
421+
const msg = caseList[i].msg;
422+
should.deepEqual({
423+
bucket: actualBucket,
424+
key: actualKey
425+
}, expect, msg);
426+
}
427+
});
373428
});
374429

375430
describe('test prepareZone with change hosts config', function () {

0 commit comments

Comments
 (0)