Sub topic 7
Chunked and resumable uploads for large files and unreliable networks
This chapter assumes you know how presigned URLs work, what an S3-style object store is, and how HTTP PUT and response headers behave. A single presigned PUT or POST is fine for small files. For a multi-gigabyte video on a mobile connection, one dropped packet near the end means starting over. Chunked uploads fix this.
The working model
In a chunked upload, the client splits a file into parts and sends each part as its own request. A failed part is retried alone. The storage service assembles the parts into one object once all of them arrive.
With S3, this feature is called multipart upload. It has three phases:
- Initiate. Your server calls
create_multipart_uploadand gets anUploadId. - Upload parts. Each part has a number from 1 to 10,000. The client sends each part to its own presigned URL and reads the
ETagresponse header. - Complete. Your server calls
complete_multipart_uploadwith the list of part numbers and ETags. S3 then makes the object visible.
Server-side code
The server starts the upload and signs one URL per part. It signs URLs for the upload_part operation, and each URL is tied to one part number.
import boto3
s3 = boto3.client("s3")
bucket, key = "example-uploads", "incoming/video-123.mp4"
upload_id = s3.create_multipart_upload(
Bucket=bucket, Key=key, ContentType="video/mp4"
)["UploadId"]
part_url = s3.generate_presigned_url(
"upload_part",
Params={
"Bucket": bucket,
"Key": key,
"UploadId": upload_id,
"PartNumber": 1,
},
ExpiresIn=3600,
)
# The client PUTs each part (at least 5 MiB, except the last) to its own
# presigned URL and records the ETag response header of every part.
s3.complete_multipart_upload(
Bucket=bucket,
Key=key,
UploadId=upload_id,
MultipartUpload={"Parts": [{"ETag": "etag-from-part-1", "PartNumber": 1}]},
)Every part except the last must be at least 5 MiB. Choose a part size that balances retry cost against request count. Between 8 and 16 MiB is common.
How recovery works
Resuming relies on one fact: S3 keeps uploaded parts until you complete or abort the upload. After a failure, the client does not restart. It asks which parts exist and sends only the missing ones.
- Retry a failed part. Re-PUT the same part number. The new data replaces the old.
- Resume after a closed tab or app restart. Store the
UploadIdand key inlocalStorageor on your server. Your server can calllist_partsto see what S3 holds, and the client uploads the rest. - Handle expiry. A presigned URL expires on its own timer. Give the client a way to request fresh URLs for the remaining parts.
Common errors
- Missing ETag in JavaScript. Browsers hide the
ETagheader from cross-origin responses unless the bucket's CORS configuration lists it underExposeHeaders. - Parts below the minimum. A non-final part under 5 MiB makes
complete_multipart_uploadfail. - Abandoned uploads. Incomplete uploads are never visible as objects, but their parts are stored and billed. Add a lifecycle rule that aborts incomplete multipart uploads after a few days.
- Wrong ordering. The list passed to
complete_multipart_uploadmust be in ascending part-number order. - Trusting the client. The final object still needs the size limit, type check, and scanning from earlier chapters. Presigned part URLs do not enforce a total size, so verify it after completion.
Other protocols
Some systems use the tus protocol or a vendor API instead of S3 multipart. The idea is the same. The client asks the server for the current offset (the number of bytes received), then continues from that point. Use S3 multipart when files go straight to S3. Consider tus when you need one resumable protocol across several storage backends.
Exercise
A user uploads a 2 GB file in 16 MiB parts, and the connection drops after 40 parts have succeeded. The user reopens the page an hour later. What does the client need, and what should it do?
Show answer
It needs the stored UploadId and key. It asks the server to call list_parts, which returns the 40 parts S3 kept. The client then uploads only the remaining parts, using fresh presigned URLs if the old ones expired. Then it sends all part numbers and ETags to complete the upload. The 40 parts are not uploaded again.