Skip to content

Commit

Permalink
分片上传增加init upload part,complete3个情况的 requestId (#335)
Browse files Browse the repository at this point in the history
* 添加 分片上传返回request id   初始化和 单片上传

* add requestid with multipart and test case

* fix test ase

* opti: test case assert

* fix node test case

* add multipart doc

* opti: test case

* case 判空

* fix test case

* fix test case
  • Loading branch information
binghaiwang authored and PeterRao committed Jan 8, 2018
1 parent b840b57 commit ceae401
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 26 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1525,8 +1525,8 @@ parameters:
- [parallel] {Number} the number of parts to be uploaded in parallel
- [partSize] {Number} the suggested size for each part
- [progress] {Function} the progress callback called after each
successful upload of one part, it will be given two parameters:
(percentage {Number}, checkpoint {Object})
successful upload of one part, it will be given three parameters:
(percentage {Number}, checkpoint {Object}, res {Object})
- [checkpoint] {Object} the checkpoint to resume upload, if this is
provided, it will continue the upload from where interrupted,
otherwise a new multipart upload will be created.
Expand Down Expand Up @@ -1559,17 +1559,19 @@ console.log(result);
var result = yield store.multipartUpload('object', '/tmp/file', {
parallel: 4,
partSize: 1024 * 1024,
progress: function* (p, cpt) {
progress: function* (p, cpt, res) {
console.log(p);
console.log(cpt);
console.log(res.headers['x-oss-request-id']);
}
});
var result = yield store.multipartUpload('object', '/tmp/file', {
checkpoint: savedCpt,
progress: function* (p, cpt) {
progress: function* (p, cpt, res) {
console.log(p);
console.log(cpt);
console.log(res.headers['x-oss-request-id']);
}
});
Expand Down
7 changes: 6 additions & 1 deletion lib/browser/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ proto.multipartUpload = function* multipartUpload(name, file, options) {
doneParts: []
};

if (options && options.progress) {
yield options.progress(0, checkpoint, result.res);
}


return yield this._resumeMultipart(checkpoint, options);
};

Expand Down Expand Up @@ -108,7 +113,7 @@ proto._resumeMultipart = function* _resumeMultipart(checkpoint, options) {
checkpoint.doneParts = doneParts;

if (options && options.progress) {
yield options.progress(doneParts.length / numParts, checkpoint);
yield options.progress(doneParts.length / numParts, checkpoint, result.res);
}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/browser/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
exports.version="4.11.2"
exports.version="4.11.3"
6 changes: 5 additions & 1 deletion lib/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ proto.multipartUpload = function* multipartUpload(name, file, options) {
doneParts: []
};

if (options && options.progress) {
yield options.progress(0, checkpoint, result.res);
}

return yield this._resumeMultipart(checkpoint, options);
};

Expand Down Expand Up @@ -108,7 +112,7 @@ proto._resumeMultipart = function* _resumeMultipart(checkpoint, options) {
checkpoint.doneParts = doneParts;

if (options && options.progress) {
yield options.progress(doneParts.length / numParts, checkpoint);
yield options.progress(doneParts.length / numParts, checkpoint, result.res);
}
};

Expand Down
47 changes: 32 additions & 15 deletions test/browser.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ timemachine.reset();
describe('browser', function () {
before(function* () {
ossConfig = {
region: stsConfig.region,
accessKeyId: stsConfig.Credentials.AccessKeyId,
accessKeySecret: stsConfig.Credentials.AccessKeySecret,
stsToken: stsConfig.Credentials.SecurityToken,
bucket: stsConfig.bucket
region: stsConfig.region,
accessKeyId: stsConfig.Credentials.AccessKeyId,
accessKeySecret: stsConfig.Credentials.AccessKeySecret,
stsToken: stsConfig.Credentials.SecurityToken,
bucket: stsConfig.bucket
};
// this.store = oss({
// region: stsConfig.region,
Expand Down Expand Up @@ -423,7 +423,7 @@ describe('browser', function () {
result.objects.map(checkObjectProperties);
assert.equal(result.nextMarker, null);
assert(!result.isTruncated);
assert.deepEqual(result.prefixes, [ this.listPrefix + 'fun/', this.listPrefix + 'other/' ]);
assert.deepEqual(result.prefixes, [this.listPrefix + 'fun/', this.listPrefix + 'other/']);

var result = yield this.store.list({
prefix: this.listPrefix + 'fun/',
Expand All @@ -447,7 +447,7 @@ describe('browser', function () {
});
});

describe('put', function() {
describe('put', function () {
before(function* () {
this.store = oss(ossConfig);
});
Expand Down Expand Up @@ -531,7 +531,7 @@ describe('browser', function () {
assert.equal(urlRes.data.toString(), result.content.toString());
});

it('should signature url with custom host ok', function() {
it('should signature url with custom host ok', function () {
var store = oss(Object.assign({}, ossConfig, {
endpoint: 'www.aliyun.com',
cname: true
Expand All @@ -543,7 +543,7 @@ describe('browser', function () {
});
});

describe('multipart', function() {
describe('multipart', function () {
before(function* () {
this.store = oss(ossConfig);
});
Expand Down Expand Up @@ -727,7 +727,7 @@ describe('browser', function () {
it('should upload file using multipart upload', function* () {
// create a file with 1M random data
// var fileName = yield utils.createTempFile('multipart-upload-file', 1024 * 1024);
var fileContent = Array(1024*1024).fill('a').join('')
var fileContent = Array(1024 * 1024).fill('a').join('')
var file = new File([fileContent], 'multipart-fallback');

var name = prefix + 'multipart/upload-file.js';
Expand All @@ -743,21 +743,38 @@ describe('browser', function () {
});
sinon.restore();
assert.equal(result.res.status, 200);
assert.equal(progress, 11);
assert.equal(progress, 12);

var object = yield this.store.get(name);
assert.equal(object.res.status, 200);

var fileBuf=new Uint8Array(fileContent.length);
for(var i=0,j=fileContent.length;i<j;++i){
fileBuf[i]=fileContent.charCodeAt(i);
var fileBuf = new Uint8Array(fileContent.length);
for (var i = 0, j = fileContent.length; i < j; ++i) {
fileBuf[i] = fileContent.charCodeAt(i);
}

assert.equal(object.content.length, fileBuf.length);
// avoid comparing buffers directly for it may hang when generating diffs
assert.deepEqual(md5(object.content), md5(fileBuf));
});

it('return requestId in init, upload part, complete', function* () {
var fileContent = Array(1024 * 1024).fill('a').join('')
var file = new File([fileContent], 'multipart-fallback');
var name = prefix + 'multipart/fallback';
var result = yield this.store.multipartUpload(name, file, {
progress: function (p, checkpoint, res) {
return function (done) {
assert.equal(true, res && Object.keys(res).length !== 0);
done();
}
}
}
);
assert.equal(true, result.res && Object.keys(result.res).length !== 0);
assert.equal(result.res.status, 200);
});

// it('should upload file using multipart upload with exception', function* () {
// // create a file with 1M random data
// var fileContent = Array(1024*1024).fill('a').join('')
Expand Down Expand Up @@ -788,7 +805,7 @@ describe('browser', function () {
});
});

describe('request time is skew', function() {
describe('request time is skew', function () {
before(function* () {
this.store = oss(ossConfig);
});
Expand Down
20 changes: 18 additions & 2 deletions test/multipart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ describe('test/multipart.test.js', function () {
}
});
assert.equal(result.res.status, 200);
assert.equal(progress, 11);
assert.equal(progress, 12);

var object = yield this.store.get(name);
assert.equal(object.res.status, 200);
Expand Down Expand Up @@ -368,7 +368,7 @@ describe('test/multipart.test.js', function () {
}
});
assert.equal(result.res.status, 200);
assert.equal(progress, 11);
assert.equal(progress, 12);

var object = yield this.store.get(name);
assert.equal(object.res.status, 200);
Expand Down Expand Up @@ -398,5 +398,21 @@ describe('test/multipart.test.js', function () {
assert.equal(result.res.status, 200);
assert.equal(result.data.Status, 'OK');
});

it('return requestId in init, upload part, complete', function* () {
var fileName = yield utils.createTempFile('multipart-upload-file', 1024 * 1024);// 1m
var name = prefix + 'multipart/upload-file';

var result = yield this.store.multipartUpload(name, fileName, {
progress: function (p, checkpoint, res) {
assert.equal(true, res && Object.keys(res).length !== 0);
}
}
);
assert.equal(true, result.res && Object.keys(result.res).length !== 0);
assert.equal(result.res.status, 200);

});

});
});
4 changes: 2 additions & 2 deletions test/wrapper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('test/wrapper.test.js', () => {

before(function* () {
this.store = OSS(config);
this.bucket = 'ali-oss-test-object-bucket-' + prefix.replace(/[\/\.]/g, '-');
this.bucket = 'ali-oss-test-wrapper-bucket-' + prefix.replace(/[\/\.]/g, '-');
this.bucket = this.bucket.substring(0, this.bucket.length - 1);
this.region = config.region;

Expand Down Expand Up @@ -76,7 +76,7 @@ describe('test/wrapper.test.js', () => {
}
}).then(function (val) {
assert.equal(val.res.status, 200);
assert.equal(count, Math.ceil(1024 / 100));
assert.equal(count, 12);

return store.get(name);
}).then(function (val) {
Expand Down

0 comments on commit ceae401

Please sign in to comment.