-
Notifications
You must be signed in to change notification settings - Fork 618
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(file-manager): multipart upload and progress bar (#3232)
- Loading branch information
Showing
43 changed files
with
1,293 additions
and
330 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
packages/api-file-manager-s3/src/multiPartUpload/CompleteMultiPartUploadUseCase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import S3 from "aws-sdk/clients/s3"; | ||
|
||
interface CompleteMultiPartUploadParams { | ||
fileKey: string; | ||
uploadId: string; | ||
} | ||
|
||
interface GetAllUploadPartsParams { | ||
Bucket: string; | ||
Key: string; | ||
UploadId: string; | ||
} | ||
|
||
export class CompleteMultiPartUploadUseCase { | ||
private readonly s3: S3; | ||
private readonly bucket: string; | ||
|
||
constructor(bucket: string, s3Client: S3) { | ||
this.bucket = bucket; | ||
this.s3 = s3Client; | ||
} | ||
|
||
async execute(params: CompleteMultiPartUploadParams) { | ||
const uploadParams = { | ||
Bucket: this.bucket, | ||
Key: params.fileKey, | ||
UploadId: params.uploadId | ||
}; | ||
|
||
const allParts = await this.getAllUploadParts(uploadParams); | ||
|
||
const s3Params = { | ||
...uploadParams, | ||
MultipartUpload: { | ||
Parts: allParts | ||
} | ||
}; | ||
|
||
return new Promise<void>((resolve, reject) => { | ||
this.s3.completeMultipartUpload(s3Params, (err, data) => { | ||
if (err) { | ||
console.error(err); | ||
reject(err); | ||
return; | ||
} | ||
|
||
console.log(data); | ||
resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
private async getAllUploadParts(params: GetAllUploadPartsParams) { | ||
const parts: S3.Parts = []; | ||
|
||
let marker: number | undefined = undefined; | ||
while (true) { | ||
const { Parts, PartNumberMarker }: S3.ListPartsOutput = await this.s3 | ||
.listParts({ | ||
...params, | ||
PartNumberMarker: marker | ||
}) | ||
.promise(); | ||
|
||
if (Parts) { | ||
Parts.forEach(part => parts.push(part)); | ||
} | ||
|
||
marker = PartNumberMarker || undefined; | ||
if (!marker) { | ||
break; | ||
} | ||
} | ||
|
||
return parts.map(part => ({ | ||
ETag: part.ETag as string, | ||
PartNumber: part.PartNumber as number | ||
})); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/api-file-manager-s3/src/multiPartUpload/CreateMultiPartUploadUseCase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import S3 from "aws-sdk/clients/s3"; | ||
import { prepareFileData } from "~/utils/prepareFileData"; | ||
|
||
interface CreateMultiPartUploadParams { | ||
file: { | ||
name: string; | ||
type: string; | ||
size: number; | ||
}; | ||
numberOfParts: number; | ||
} | ||
|
||
export class CreateMultiPartUploadUseCase { | ||
private readonly s3: S3; | ||
private readonly bucket: string; | ||
|
||
constructor(bucket: string, s3Client: S3) { | ||
this.bucket = bucket; | ||
this.s3 = s3Client; | ||
} | ||
|
||
async execute(params: CreateMultiPartUploadParams) { | ||
const file = prepareFileData(params.file); | ||
|
||
const s3Params = { Bucket: this.bucket, Key: file.key }; | ||
|
||
const { UploadId } = await this.s3.createMultipartUpload(s3Params).promise(); | ||
|
||
const parts = await Promise.all( | ||
Array.from({ length: params.numberOfParts }).map((_, index) => { | ||
return this.s3 | ||
.getSignedUrlPromise("uploadPart", { | ||
...s3Params, | ||
UploadId, | ||
PartNumber: index + 1, | ||
// URL expires after 24 hours. | ||
Expires: 86400 | ||
}) | ||
.then(url => ({ | ||
url, | ||
partNumber: index + 1 | ||
})); | ||
}) | ||
); | ||
|
||
return { | ||
file, | ||
uploadId: UploadId, | ||
parts | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.