Skip to content

Commit

Permalink
fix: multipartupload task unhandle abort error (#849)
Browse files Browse the repository at this point in the history
  • Loading branch information
weiyie authored Aug 14, 2020
1 parent c22b607 commit 8f9c61b
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 4 deletions.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2732,6 +2732,33 @@ const result2 = await store.multipartUpload('object', '/tmp/file', {

```
- multipartUpload with abort
>tips: abort multipartUpload support on node and browser
```js

//start upload
let abortCheckpoint;
store.multipartUpload('object', '/tmp/file', {
progress: function (p, cpt, res) {
abortCheckpoint = cpt;
}
}).then(res => {
// do something
}.catch(err => {
//if abort will catch abort event
if (err.name === 'abort') {
// handle abort
console.log('error: ', err.message)
}
}))

// abort
store.abortMultipartUpload(abortCheckpoint.name, abortCheckpoint.uploadId)

```
- multipartUpload with cancel
>tips: cancel multipartUpload support on node and browser
Expand Down Expand Up @@ -2873,6 +2900,36 @@ const result = await store.multipartUploadCopy('object', {
console.log(result);

```
- multipartUploadCopy with abort
```js

//start upload
let abortCheckpoint;
store.multipartUploadCopy('object', {
sourceKey: 'sourceKey',
sourceBucketName: 'sourceBucketName'
}, {
progress: function (p, cpt, res) {
abortCheckpoint = cpt;
}
}).then(res => {
// do something
}.catch(err => {
//if abort will catch abort event
if (err.name === 'abort') {
// handle abort
console.log('error: ', err.message)
}
}))

//the other event to abort, for example: click event
//to abort upload must use the same client instance
store.abortMultipartUpload(abortCheckpoint.name, abortCheckpoint.uploadId)

```
- multipartUploadCopy with cancel
```js
Expand Down
13 changes: 12 additions & 1 deletion lib/browser/managed-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,15 @@ proto._resumeMultipart = async function _resumeMultipart(checkpoint, options) {
size: pi.end - pi.start
};

const result = await self._uploadPart(name, uploadId, partNo, data);
let result;
try {
result = await self._uploadPart(name, uploadId, partNo, data);
} catch (error) {
if (error.status === 404) {
throw self._makeAbortEvent();
}
throw error;
}
if (!self.isCancel() && !multipartFinish) {
checkpoint.doneParts.push({
number: partNo,
Expand Down Expand Up @@ -195,6 +203,9 @@ proto._resumeMultipart = async function _resumeMultipart(checkpoint, options) {
}));
multipartFinish = true;

const abortEvent = jobErr.find(err => err.name === 'abort');
if (abortEvent) throw abortEvent;

if (this.isCancel()) {
uploadPartJob = null;
throw this._makeCancelEvent();
Expand Down
15 changes: 13 additions & 2 deletions lib/common/multipart-copy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-async-promise-executor */

const debug = require('debug')('ali-oss:multipart-copy');
const copy = require('copy-to');
Expand Down Expand Up @@ -144,8 +145,15 @@ proto._resumeMultipartCopy = async function _resumeMultipartCopy(checkpoint, sou
const pi = partOffs[partNo - 1];
const range = `${pi.start}-${pi.end - 1}`;

const result = await self.uploadPartCopy(name, uploadId, partNo, range, source, uploadPartCopyOptions);

let result;
try {
result = await self.uploadPartCopy(name, uploadId, partNo, range, source, uploadPartCopyOptions);
} catch (error) {
if (error.status === 404) {
throw self._makeAbortEvent();
}
throw error;
}
if (!self.isCancel()) {
debug(`content-range ${result.res.headers['content-range']}`);
doneParts.push({
Expand Down Expand Up @@ -185,6 +193,9 @@ proto._resumeMultipartCopy = async function _resumeMultipartCopy(checkpoint, sou
// upload in parallel
const errors = await this._parallelNode(todo, parallel, uploadPartJob, sourceData);

const abortEvent = errors.find(err => err.name === 'abort');
if (abortEvent) throw abortEvent;

if (this.isCancel()) {
throw this._makeCancelEvent();
}
Expand Down
10 changes: 10 additions & 0 deletions lib/common/parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,13 @@ proto._makeCancelEvent = function _makeCancelEvent() {
};
return cancelEvent;
};

// abort is not error , so create an object
proto._makeAbortEvent = function _makeAbortEvent() {
const abortEvent = {
status: 0,
name: 'abort',
message: 'upload task has been abort'
};
return abortEvent;
};
13 changes: 12 additions & 1 deletion lib/managed-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,16 @@ proto._resumeMultipart = async function _resumeMultipart(checkpoint, options) {
stream: self._createStream(file, pi.start, pi.end),
size: pi.end - pi.start
};
let result;
try {
result = await self._uploadPart(name, uploadId, partNo, data);
} catch (error) {
if (error.status === 404) {
throw self._makeAbortEvent();
}
throw error;
}

const result = await self._uploadPart(name, uploadId, partNo, data);
if (!self.isCancel()) {
doneParts.push({
number: partNo,
Expand Down Expand Up @@ -169,6 +177,9 @@ proto._resumeMultipart = async function _resumeMultipart(checkpoint, options) {
// upload in parallel
const jobErr = await this._parallelNode(todo, parallel, uploadPartJob);

const abortEvent = jobErr.find(err => err.name === 'abort');
if (abortEvent) throw abortEvent;

if (this.isCancel()) {
uploadPartJob = null;
throw this._makeCancelEvent();
Expand Down

0 comments on commit 8f9c61b

Please sign in to comment.