Skip to content

Commit

Permalink
fix(storage): missing Size in listParts output (#11559)
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF authored Jun 29, 2023
2 parents fe05494 + 679d78a commit 4085319
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/storage/__tests__/AwsClients/S3/cases/listParts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ const listPartsHappyCase: ApiFunctionalTestCase<typeof listParts> = [
'<Part>' +
'<PartNumber>1</PartNumber>' +
'<ETag>etag1</ETag>' +
'<Size>5242880</Size>' +
'</Part>' +
'<Part>' +
'<PartNumber>2</PartNumber>' +
'<ETag>etag2</ETag>' +
'<Size>1024</Size>' +
'</Part>' +
'</ListPartsResult>',
},
Expand All @@ -50,10 +52,12 @@ const listPartsHappyCase: ApiFunctionalTestCase<typeof listParts> = [
{
PartNumber: 1,
ETag: 'etag1',
Size: 5242880,
},
{
PartNumber: 2,
ETag: 'etag2',
Size: 1024,
},
],
},
Expand Down
73 changes: 73 additions & 0 deletions packages/storage/__tests__/providers/AWSS3UploadTask-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,79 @@ describe('resumable upload task test', () => {
);
});

test('Should complete upload if all parts have been uploaded on resume', done => {
const file = new File(['TestFileContent'], 'testFileName');
Object.defineProperty(file, 'size', { value: 25048576 });
const emitter = new events.EventEmitter();
const input: AWSS3UploadTaskParams = {
file,
// s3Client: new S3Client(testOpts),
s3Config: defaultS3Config,
emitter: emitter,
storage: mockLocalStorage,
level: 'public',
params: {
Bucket: 'bucket',
Key: 'key',
},
prefixPromise: Promise.resolve('prefix'),
};
(listParts as jest.Mock).mockResolvedValue({
Parts: [
{
PartNumber: 1,
Size: 25048576 / 2,
ETag: 'etag-1',
},
{
PartNumber: 2,
Size: 25048576 / 2,
ETag: 'etag-2',
},
],
});
(createMultipartUpload as jest.Mock).mockResolvedValue({
UploadId: 'uploadId',
});
(listObjectsV2 as jest.Mock).mockResolvedValue({
Contents: [{ Key: 'prefix' + input.params.Key, Size: 25048576 }],
});
(completeMultipartUpload as jest.Mock).mockResolvedValue({
Key: input.params.Key,
});
const fileMetadata: FileMetadata = {
bucket: 'bucket',
key: 'key',
lastTouched: Date.now(),
uploadId: 'uploadId',
fileName: file.name,
};
const fileId = [
file.name,
file.lastModified,
file.size,
file.type,
input.params.Bucket,
input.level,
input.params.Key,
].join('-');
const cachedUploadTasks = {
[fileId]: fileMetadata,
};
mockLocalStorage.setItem(
UPLOADS_STORAGE_KEY,
JSON.stringify(cachedUploadTasks)
);
const uploadTask = new AWSS3UploadTask(input);
// kick off the upload task
uploadTask.resume();

emitter.on(TaskEvents.UPLOAD_COMPLETE, () => {
expect(completeMultipartUpload).toBeCalledTimes(1);
done();
});
});

test('upload a body that exceeds the size of default part size and parts count', done => {
const testUploadId = 'testUploadId';
let buffer: ArrayBuffer;
Expand Down
1 change: 1 addition & 0 deletions packages/storage/src/AwsClients/S3/listParts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const deserializeCompletedPartList = (input: any[]): CompletedPart[] =>
map(item, {
PartNumber: ['PartNumber', deserializeNumber],
ETag: 'ETag',
Size: ['Size', deserializeNumber],
})
);

Expand Down
6 changes: 5 additions & 1 deletion packages/storage/src/providers/AWSS3UploadTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,11 @@ export class AWSS3UploadTask implements UploadTask {
this.uploadId = uploadId;
this.queued = this._createParts();
this._initCachedUploadParts(parts);
this._startUpload();
if (this._isDone()) {
this._completeUpload();
} else {
this._startUpload();
}
} else {
if (!this.uploadId) {
const uploadId = await this._initMultipartUpload();
Expand Down

0 comments on commit 4085319

Please sign in to comment.