Skip to content

Commit

Permalink
fix batch rs host (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihsai0 authored Dec 11, 2023
1 parent 802744b commit 5188200
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 10 deletions.
61 changes: 51 additions & 10 deletions qiniu/storage/rs.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,21 +550,51 @@ function listPrefixReqV2 (mac, config, bucket, options, callbackFunc) {

// 批量文件管理请求,支持stat,chgm,chtype,delete,copy,move
BucketManager.prototype.batch = function (operations, callbackFunc) {
var scheme = this.config.useHttpsDomain ? 'https://' : 'http://';
var requestURI = scheme + conf.RS_HOST + '/batch';
var reqParams = {
if (!operations.length) {
callbackFunc(new Error('Empty operations'), null, null)
}

let bucket;
for (const op of operations) {
const [, , entry] = op.split('/');
if (!entry) {
continue;
}
[bucket] = util.decodedEntry(entry);
if (bucket) {
break;
}
}
if (!bucket) {
callbackFunc(new Error('Empty bucket'));
return;
}

util.prepareZone(this, this.mac.accessKey, bucket, function (err, ctx) {
if (err) {
callbackFunc(err, null, null);
return;
}
batchReq(ctx.mac, ctx.config, operations, callbackFunc);
});
};

function batchReq (mac, config, operations, callbackFunc) {
const scheme = config.useHttpsDomain ? 'https://' : 'http://';
const requestURI = scheme + config.zone.rsHost + '/batch';
const reqParams = {
op: operations
};
var reqBody = querystring.stringify(reqParams);
const reqBody = querystring.stringify(reqParams);
rpc.postWithOptions(
requestURI,
reqBody,
{
mac: this.mac
mac: mac
},
callbackFunc
);
};
}

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

// 解冻归档存储文件
BucketManager.prototype.restoreAr = function (entry, freezeAfterDays, callbackFunc) {
var scheme = this.config.useHttpsDomain ? 'https://' : 'http://';
var requestURI = scheme + conf.RS_HOST + '/restoreAr/' + util.urlsafeBase64Encode(entry) + '/freezeAfterDays/' + freezeAfterDays;
const [bucket] = entry.split(':');
util.prepareZone(this, this.mac.accessKey, bucket, function (err, ctx) {
if (err) {
callbackFunc(err, null, null);
return;
}
restoreArReq(ctx.mac, ctx.config, entry, freezeAfterDays, callbackFunc);
});
};

function restoreArReq (mac, config, entry, freezeAfterDays, callbackFunc) {
const scheme = config.useHttpsDomain ? 'https://' : 'http://';
const requestURI = scheme + config.zone.rsHost + '/restoreAr/' + util.urlsafeBase64Encode(entry) + '/freezeAfterDays/' + freezeAfterDays;
rpc.postWithOptions(
requestURI,
null,
{
mac: this.mac
mac: mac
},
callbackFunc
);
};
}

// 上传策略
// @link https://developer.qiniu.com/kodo/manual/1206/put-policy
Expand Down
5 changes: 5 additions & 0 deletions qiniu/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ exports.encodedEntry = function (bucket, key) {
return exports.urlsafeBase64Encode(strToEncode);
};

exports.decodedEntry = function (entry) {
const [bucket, key] = exports.urlSafeBase64Decode(entry).split(':');
return [bucket, key];
};

// Get accessKey from uptoken
exports.getAKFromUptoken = function (uploadToken) {
var sepIndex = uploadToken.indexOf(':');
Expand Down
55 changes: 55 additions & 0 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,61 @@ describe('test util functions', function () {
should.equal(actual, expect, msg);
}
});

it('test decodedEntry', function () {
const caseList = [
{
msg: 'normal',
expect: {
bucket: 'qiniuphotos',
key: 'gogopher.jpg'
},
entry: 'cWluaXVwaG90b3M6Z29nb3BoZXIuanBn'
},
{
msg: 'key empty',
expect: {
bucket: 'qiniuphotos',
key: ''
},
entry: 'cWluaXVwaG90b3M6'
},
{
msg: 'key undefined',
expect: {
bucket: 'qiniuphotos',
key: undefined
},
entry: 'cWluaXVwaG90b3M='
},
{
msg: 'key need replace plus symbol',
expect: {
bucket: 'qiniuphotos',
key: '012ts>a'
},
entry: 'cWluaXVwaG90b3M6MDEydHM-YQ=='
},
{
msg: 'key need replace slash symbol',
expect: {
bucket: 'qiniuphotos',
key: '012ts?a'
},
entry: 'cWluaXVwaG90b3M6MDEydHM_YQ=='
}
];

for (let i = 0; i < caseList.length; i++) {
const [actualBucket, actualKey] = qiniu.util.decodedEntry(caseList[i].entry);
const expect = caseList[i].expect;
const msg = caseList[i].msg;
should.deepEqual({
bucket: actualBucket,
key: actualKey
}, expect, msg);
}
});
});

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

0 comments on commit 5188200

Please sign in to comment.