Sub topic 13
Uploading large objects with multipart upload
This chapter assumes you can create a bucket, upload objects with the AWS CLI or boto3, and read basic Python. You should also know what an S3 key is and have a rough idea of IAM permissions. Earlier chapters cover those.
Why single-request uploads run out of room
A single PutObject request sends the whole object in one HTTP request. That works for small files, but it has two limits:
- The maximum object size for one
PutObjectis 5 GB. - If the connection drops at 95%, you start over from byte zero.
Multipart upload removes both limits. It splits an object into parts, uploads each part as its own request, and asks S3 to assemble them into one object. The maximum object size becomes 5 TB.
| Property | Single `PutObject` | Multipart upload |
|---|---|---|
| Maximum object size | 5 GB | 5 TB |
| Retry after failure | Whole object | Failed part only |
| Parallelism | One request | Many parts at once |
| Needs object size known up front | Yes | No |
| Extra cleanup needed | No | Yes (incomplete uploads) |
AWS recommends considering multipart upload once objects reach about 100 MB.
The three-step protocol
Every multipart upload follows the same sequence of API calls.
CreateMultipartUploadstarts the upload. S3 returns an upload ID, which identifies this upload in every later call.UploadPartsends one part. You give it the upload ID and a part number from 1 to 10,000. S3 returns an ETag for the part.CompleteMultipartUploadlists every part number with its ETag. S3 joins the parts in part-number order and creates the object.
If you decide to stop, AbortMultipartUpload discards the uploaded parts.
Until step 3 succeeds, no object exists at that key. A reader calling GetObject sees the previous version of the key, or a 404 if there was none. The upload also does not appear in a normal object listing.
Part size rules
These limits shape how you choose a part size.
- Each part must be at least 5 MiB, except the last part, which can be any size.
- Each part can be at most 5 GiB.
- An upload can have at most 10,000 parts.
The 10,000-part cap sets a floor on part size for very large objects. To upload a 1 TiB file, the part size must be at least 1 TiB / 10,000, which is about 105 MiB. A 64 MiB part size would allow only about 625 GiB, and the upload would fail partway through. Choose the part size from the largest object you expect, not from the typical one.
Letting boto3 handle it
You rarely call the three APIs by hand. The managed transfer methods in boto3 (upload_file, upload_fileobj, download_file) switch to multipart automatically above a size threshold. They also upload parts in parallel threads and retry failed parts. You control this with TransferConfig.
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,
)This code does the following:
- Files under 64 MiB go as a single
PutObject. - Larger files are cut into 64 MiB parts.
- With default settings, up to 10 threads upload parts at the same time. You can change that with
max_concurrency.
The AWS CLI behaves the same way. aws s3 cp and aws s3 sync use multipart for files over 8 MB by default. You can change this in your CLI configuration:
aws configure set default.s3.multipart_threshold 64MB
aws configure set default.s3.multipart_chunksize 64MB
aws configure set default.s3.max_concurrent_requests 20Using the low-level API directly
You need the low-level calls when you want to control the flow yourself. Examples are uploading parts from different machines, or resuming an upload after a process restart. This example uploads a file in sequence, which keeps it short. A real job would send parts in parallel.
import boto3
s3 = boto3.client("s3")
bucket = "my-example-bucket-123456789012"
key = "backups/backup.tar"
part_size = 64 * 1024 * 1024
upload = s3.create_multipart_upload(Bucket=bucket, Key=key)
upload_id = upload["UploadId"]
parts = []
try:
with open("backup.tar", "rb") as f:
part_number = 1
while True:
data = f.read(part_size)
if not data:
break
result = s3.upload_part(
Bucket=bucket,
Key=key,
UploadId=upload_id,
PartNumber=part_number,
Body=data,
)
parts.append({"PartNumber": part_number, "ETag": result["ETag"]})
part_number += 1
s3.complete_multipart_upload(
Bucket=bucket,
Key=key,
UploadId=upload_id,
MultipartUpload={"Parts": parts},
)
except Exception:
s3.abort_multipart_upload(Bucket=bucket, Key=key, UploadId=upload_id)
raiseThree details matter here:
- The
Partslist passed tocomplete_multipart_uploadmust be in ascending part-number order, and each ETag must match what S3 returned. - Uploading a part number a second time replaces the earlier part with that number. This is how you retry one part safely.
- The
exceptblock aborts the upload so that the stored parts do not linger. Without it, a crash in the loop leaves the parts in the bucket.
The hidden cost of incomplete uploads
Parts that are uploaded but never completed or aborted stay in the bucket, and you pay storage for them. They do not show up in list-objects-v2, in the console object view, or in most simple size checks. This is the most common surprise with multipart upload. A bucket can hold terabytes of orphaned parts that nobody can see in a normal listing.
To find them, list the in-progress uploads:
aws s3api list-multipart-uploads \
--bucket my-example-bucket-123456789012To remove 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-IDThe better fix is a lifecycle rule that aborts incomplete uploads automatically. This extends the lifecycle rules from the previous chapter:
{
"Rules": [
{
"ID": "abort-incomplete-multipart",
"Status": "Enabled",
"Filter": { "Prefix": "" },
"AbortIncompleteMultipartUpload": { "DaysAfterInitiation": 7 }
}
]
}Save it as abort.json and apply it:
aws s3api put-bucket-lifecycle-configuration \
--bucket my-example-bucket-123456789012 \
--lifecycle-configuration file://abort.jsonSeven days is a common choice. It is long enough for a slow upload to finish and short enough to limit waste. Add this rule to most buckets that receive large uploads.
ETags and integrity
For a single-request upload with no KMS encryption, the ETag is the MD5 hash of the object. For a multipart object it is not. S3 computes the MD5 of each part, concatenates those hashes, hashes the result, and appends a dash and the part count. An ETag such as 9b2cf535f27731c974343645a3985328-12 means the object was uploaded in 12 parts.
Two consequences follow:
- You cannot compare a multipart ETag with a local
md5sumto verify the upload. - The same file uploaded with a different part size gets a different ETag, even though the content is identical.
For integrity checks, use S3's additional checksums, such as SHA-256 or CRC32C. You request one at upload time, and S3 verifies each part against it. Recent versions of the AWS SDKs and CLI add a default checksum to uploads, but the exact default depends on the version. Check what your version sends rather than assuming.
Interactions with other features
- Versioning. The completed object becomes a new version. The parts themselves are not versions, and S3 does not bill them as noncurrent versions.
- Encryption. Every part is encrypted with the encryption settings given at
CreateMultipartUpload. With SSE-KMS, each part upload calls KMS, which adds to KMS request cost. - Permissions. The caller needs
s3:PutObjectfor the upload calls. To list or abort uploads, the caller needss3:ListBucketMultipartUploadson the bucket ands3:AbortMultipartUploadon the objects. - Request cost. Each part is a billable
PUTrequest. A 1 TiB object in 8 MiB parts needs 131,072 requests, which is above the 10,000-part cap, so that choice would not work anyway. A sensible part size also keeps request charges low. - Presigned URLs. You can presign individual
UploadPartcalls. A backend can create the upload, hand each browser a presigned URL per part, and then complete the upload itself.
Choosing a part size
There is a trade-off between small and large parts.
- Small parts mean cheaper retries, because a failure resends little data. They also mean more requests and a higher chance of reaching the 10,000-part cap.
- Large parts mean fewer requests and less overhead. A failed part costs more to resend, and each thread holds more data in memory.
A workable rule is 16 to 64 MiB for files up to a few hundred GiB. For larger files, divide the maximum size by 10,000, round up, and add some margin. Memory use is roughly max_concurrency × part size, so 10 threads at 64 MiB hold about 640 MiB in flight.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `EntityTooSmall` on complete | A part other than the last is under 5 MiB | Raise the part size |
| `InvalidPart` on complete | A part number or ETag in the list does not match what S3 holds | Use the exact ETags returned by `UploadPart` |
| `InvalidPartOrder` on complete | Parts listed out of order | Sort by part number |
| `NoSuchUpload` | The upload was aborted, completed, or expired by a lifecycle rule | Start a new upload |
| Storage bill higher than object sizes suggest | Orphaned incomplete uploads | List uploads and add an abort rule |
Exercise
You need to upload a single 2 TiB file with the low-level API. You plan to use 100 MiB parts. Will this work? What part size would you pick instead, and what lifecycle setting should you add to the bucket?
Show answer
It will not work. 2 TiB is 2,097,152 MiB, and 100 MiB parts would need about 20,972 parts, which exceeds the 10,000-part limit. The minimum part size is 2,097,152 MiB / 10,000, about 210 MiB. Pick something with margin, such as 256 MiB, which gives 8,192 parts and stays under the 5 GiB per-part limit. Also add a lifecycle rule with AbortIncompleteMultipartUpload (for example DaysAfterInitiation of 7) so that parts from failed attempts are deleted automatically instead of billing you silently.
Exercise
A teammate compares the ETag of a 300 MiB object in S3 with the output of md5sum on the local file, and they differ. The ETag ends in "-5". Is the upload corrupt?
Show answer
Not necessarily. The "-5" suffix shows that the object was uploaded as 5 parts, and a multipart ETag is a hash of the part hashes rather than the MD5 of the whole file. To verify integrity, compare against an S3 additional checksum such as SHA-256 or CRC32C, or recompute the multipart ETag locally using the same part size.