Sub topic 13
Uploading large objects with multipart upload
This chapter assumes you can run AWS CLI commands, read basic Python, and have credentials that can write to a bucket you own. It builds on the upload example from the tooling chapter.
Why multipart upload exists
A single PUT request can send at most 5 GB, and one failed connection means starting over. Multipart upload splits an object into parts. Each part is uploaded as its own request, and S3 joins the parts into one object when you finish.
This gives you three things:
- Parts can be uploaded in parallel, which uses more of your bandwidth.
- A failed part can be retried alone, so you do not resend the whole file.
- Objects up to 5 TB are possible, which a single PUT cannot reach.
How it works
There are three steps. You start the upload and receive an upload ID. You upload each part with a part number and keep the ETag that S3 returns for it. You then complete the upload by sending the list of part numbers and ETags, and S3 assembles the object.
The limits you need to know:
| Limit | Value |
|---|---|
| Maximum object size | 5 TB |
| Maximum number of parts | 10,000 |
| Part size | 5 MiB to 5 GiB |
| Size of the last part | Can be smaller than 5 MiB |
Because of the 10,000-part cap, the part size sets your maximum object size. With 64 MiB parts you can upload up to about 625 GiB.
Letting the SDK do it
You rarely call these APIs by hand. The upload_file method in boto3 and the aws s3 cp command both switch to multipart automatically above a size threshold. You can tune the behavior.
import boto3
from boto3.s3.transfer import TransferConfig
s3 = boto3.client("s3")
config = TransferConfig(
multipart_threshold=64 * 1024 * 1024,
multipart_chunksize=64 * 1024 * 1024,
)
s3.upload_file(
"backup.tar",
"my-example-bucket-123456789012",
"backups/backup.tar",
Config=config,
)Files smaller than 64 MiB go up in a single request. Larger files are sent in 64 MiB parts. TransferConfig also accepts max_concurrency, which sets how many parts upload at once.
Incomplete uploads cost money
If an upload starts and never completes, S3 keeps the uploaded parts and bills you for their storage. They do not appear in a normal object listing, so they are easy to miss. List them with the CLI:
aws s3api list-multipart-uploads \
--bucket my-example-bucket-123456789012To stop one, abort it with its key and upload ID:
aws s3api abort-multipart-upload \
--bucket my-example-bucket-123456789012 \
--key backups/backup.tar \
--upload-id EXAMPLE_UPLOAD_IDDo not rely on remembering to do this. A lifecycle rule can clean up automatically:
{
"Rules": [
{
"ID": "abort-stale-multipart-uploads",
"Status": "Enabled",
"Filter": { "Prefix": "" },
"AbortIncompleteMultipartUpload": { "DaysAfterInitiation": 7 }
}
]
}Save it as lifecycle.json and apply it with aws s3api put-bucket-lifecycle-configuration --bucket my-example-bucket-123456789012 --lifecycle-configuration file://lifecycle.json. Note that this call replaces the whole lifecycle configuration of the bucket, so merge this rule into any rules you already have.
Common errors
EntityTooSmall: a part other than the last was under 5 MiB.InvalidPart: the part list in the complete call has a wrong part number or ETag.NoSuchUpload: the upload ID was aborted, already completed, or is wrong.AccessDeniedon a large file only: the identity may lacks3:PutObjectfor the key, ors3:AbortMultipartUploadands3:ListMultipartUploadPartsfor cleanup.
Exercise
You upload a 200 GiB file with a part size of 16 MiB. Will it work, and what would you change?
Show answer
It will fail. 200 GiB at 16 MiB per part needs 12,800 parts, which is over the 10,000-part limit. Increase the part size, for example to 32 MiB (6,400 parts) or larger.
Exercise
Your bill shows storage growing, but listing the bucket with listobjectsv2 shows no new objects. What is a likely cause, and how do you prevent it?
Show answer
Incomplete multipart uploads are holding parts that normal listings do not show. Find them with list-multipart-uploads, abort them, and add a lifecycle rule with AbortIncompleteMultipartUpload.