Sub topic 4
Working with S3 from the CLI and an SDK
This chapter assumes you have the AWS CLI v2 and Python with boto3 installed, and that your credentials are configured (for example with aws configure or a role). It also assumes you know what a bucket and a key are from earlier chapters.
How the tools reach S3
The CLI and boto3 both call the same S3 HTTPS API. Each request is signed with credentials found by a lookup chain: environment variables, then the shared config and credentials files, then a role attached to the compute environment. Because the tools share this chain, a request that works in the CLI uses the same identity as your Python code, as long as both use the same profile.
Two CLI command sets
The CLI has two layers for S3:
aws s3has high-level commands such ascp,sync,lsandrm. They handle multipart uploads and recursion for you.aws s3apimaps one-to-one onto API operations such ascreate-bucketandput-public-access-block. Use it when you need a specific setting.
Outside us-east-1, create-bucket needs a LocationConstraint that matches the Region. This example creates a bucket and turns on all four Block Public Access settings.
aws s3api create-bucket \
--bucket my-example-bucket-123456789012 \
--region eu-west-1 \
--create-bucket-configuration LocationConstraint=eu-west-1
aws s3api put-public-access-block \
--bucket my-example-bucket-123456789012 \
--public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=trueDay-to-day work then uses the high-level commands:
aws s3 cp report.csv s3://my-example-bucket-123456789012/reports/2026/report.csv
aws s3 ls s3://my-example-bucket-123456789012/reports/ --recursive
aws s3 cp s3://my-example-bucket-123456789012/reports/2026/report.csv report-copy.csv
aws s3 rm s3://my-example-bucket-123456789012/reports/2026/report.csvThe same workflow in boto3
This script uploads a file under a prefixed key, lists the keys under that prefix, and downloads the object to a new local file.
import boto3
s3 = boto3.client("s3")
bucket = "my-example-bucket-123456789012"
s3.upload_file("report.csv", bucket, "reports/2026/report.csv")
response = s3.list_objects_v2(Bucket=bucket, Prefix="reports/")
for obj in response.get("Contents", []):
print(obj["Key"], obj["Size"])
s3.download_file(bucket, "reports/2026/report.csv", "report-copy.csv")upload_file and download_file are managed transfers. They read from and write to local paths, and they split large files into parts automatically. response.get("Contents", []) matters: when no keys match, the Contents key is missing from the response.
Common errors
| Error | Usual cause |
|---|---|
| `NoSuchBucket` | Wrong bucket name, or the client is pointed at the wrong Region or account. |
| `AccessDenied` | The identity lacks an IAM permission, or a bucket policy or Block Public Access denies the request. |
| `IllegalLocationConstraintException` | The `LocationConstraint` is missing or does not match `--region`. |
| `BucketAlreadyExists` | Bucket names are global, so another account already owns that name. |
Exercise
Your Python script calls list_objects_v2 on a prefix that you know holds 5,000 objects, but it prints only 1,000 keys and no error. What is the cause, and how do you fix it?
Show answer
S3 returns at most 1,000 keys per list call, and the response includes a continuation token for the rest. Fix it by using s3.get_paginator("list_objects_v2") and iterating over its pages, or by passing ContinuationToken yourself in a loop.