boto3 Fundamentals Basics
Seven examples to get you started with boto3, the AWS SDK for Python: five basic and two intermediate.
Busque em todas as páginas da documentação
Seven examples to get you started with boto3, the AWS SDK for Python: five basic and two intermediate.
Because this section is about the boto3 library itself, every example is plain Python. There is no TypeScript pairing here, since these are boto3's own conventions.
pip install boto3 (boto3 1.43.x on Python 3.10+; Python 3.9 reached end of life on 2026-04-29).aws configure, environment variables, or an IAM role. boto3 finds them automatically through its default chain.AWS_REGION (or AWS_DEFAULT_REGION), or pass region_name per client.boto3 is a single package that covers every AWS service.
# pip install boto3
import boto3
print(boto3.__version__) # e.g. 1.43.xrequirements.txt so builds are reproducible.Related: botocore Under the Hood - the engine beneath boto3.
The starting point for every call is a service client bound to a region.
import boto3
s3 = boto3.client("s3", region_name="us-east-1")
print(type(s3)) # <class 'botocore.client.S3'>boto3.client is one factory for every service, selected by the string name ("s3", "dynamodb", "sqs").botocore.client.S3.AWS_REGION, so you may omit region_name here.Related: boto3's Session, Client & Resource Model - where the client comes from.
Call an operation method on the client and get a structured response back.
import boto3
s3 = boto3.client("s3")
response = s3.list_buckets()
for bucket in response["Buckets"]:
print(bucket["Name"])list_buckets for the ListBuckets API).dict whose keys mirror the AWS API reference in PascalCase.ResponseMetadata key with the request id and HTTP status.Related: boto3 Fundamentals Best Practices - habits to apply from the first call.
Responses are ordinary dicts, so use dict access and .get() for optional keys.
import boto3
s3 = boto3.client("s3")
head = s3.head_object(Bucket="my-bucket", Key="report.pdf")
print(head["ContentLength"]) # required key, present on success
print(head.get("ContentType", "?")) # optional key, guard with .get()
print(head["ResponseMetadata"]["RequestId"])HeadObject fetches metadata only, without downloading the object body.KeyError, so reach for .get() when a field may be absent.ResponseMetadata is always present and is where you find the request id for logging.AWS calls fail as ClientError, which you catch and inspect.
import boto3
from botocore.exceptions import ClientError
s3 = boto3.client("s3")
try:
s3.get_object(Bucket="my-bucket", Key="missing.txt")
except ClientError as err:
code = err.response["Error"]["Code"] # e.g. "NoSuchKey"
rid = err.response["ResponseMetadata"]["RequestId"]
print(code, rid)ClientError from botocore.exceptions covers API-level errors for every service.err.response["Error"]["Code"] and branch on it.err.response dict carries the request id, useful for AWS support cases.Related: Anatomy of an AWS API Request - what the request id identifies.
Many list operations return one page at a time; paginators walk them for you.
import boto3
s3 = boto3.client("s3")
paginator = s3.get_paginator("list_objects_v2")
for page in paginator.paginate(Bucket="my-bucket", Prefix="logs/"):
for obj in page.get("Contents", []):
print(obj["Key"], obj["Size"])get_paginator takes the operation name and hides the ContinuationToken bookkeeping.Contents with .get(..., []) because an empty page omits the key entirely.Prefix server-side so you fetch only the keys you need.Related: boto3 Fundamentals Best Practices - why paginators beat manual loops.
The same loop applies to every service; only the client name and operation change.
import boto3
ddb = boto3.client("dynamodb", region_name="us-east-1")
ddb.put_item(
TableName="Visits",
Item={"id": {"S": "user-7"}, "count": {"N": "1"}},
)"dynamodb") and new methods (put_item), nothing more.{"S": ...}, {"N": ...}).Related: Low-Level Clients vs the Resources API - when a higher-level object layer helps.
Stack versions: This page was written for boto3 1.43.x on Python 3.10+ (and the AWS SDK for JavaScript v3, Node.js 18+, where contrasted).
Revisado por Chris St. John·Última atualização: 23 de jul. de 2026