Skip to content

Instantly share code, notes, and snippets.

@danicunhac
Last active September 20, 2022 00:49
Show Gist options
  • Save danicunhac/b458cf1fbb40482b74254f18c8492ad3 to your computer and use it in GitHub Desktop.
Save danicunhac/b458cf1fbb40482b74254f18c8492ad3 to your computer and use it in GitHub Desktop.
Algorithm for creating a multipart upload to AWS S3
const { UploadId: uploadId } = await s3
.createMultipartUpload({
Bucket: `${process.env.BUCKET_NAME}`,
Key: key,
})
.promise()
async function uploadPart(
partNumber: number,
uploadId: string,
body: Buffer
) {
const partParams: S3.UploadPartRequest = {
Bucket: `${process.env.BUCKET_NAME}`,
Key: key,
UploadId: `${uploadId}`,
PartNumber: partNumber,
Body: body,
}
const uploadedPart = await s3.uploadPart(partParams).promise()
return {
ETag: uploadedPart.ETag,
PartNumber: partNumber,
}
}
const promisedParts = []
for (let progress = 0; progress < body.length; progress += partSize) {
const partNumber = progress / partSize + 1
const promisedPart = uploadPart(partNumber, String(uploadId), body)
promisedParts.push(promisedPart)
}
const parts = await Promise.all(promisedParts)
const result = await s3
.completeMultipartUpload({
Bucket: `${process.env.BUCKET_NAME}`,
Key: key,
UploadId: `${uploadId}`,
MultipartUpload: {
Parts: parts,
},
})
.promise()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment