Type annotations for boto3 S3
This library provides PEP 561 compliant type annotations (stub files) for the `boto3` S3 service, generated by `mypy-boto3-builder`. It enhances static analysis, auto-completion, and type checking in IDEs and tools like `mypy` for code interacting with AWS S3 via `boto3`, addressing the dynamic nature of `boto3` clients and resources.
Warnings
- breaking Python 3.8 support has been removed in `mypy-boto3-builder` version 8.12.0, which generates this package. Projects using `types-boto3-s3` must now target Python 3.9 or newer.
- breaking The `mypy-boto3-builder` (version 8.9.0) introduced changes to `TypeDef` naming conventions for service operations, specifically shortening packed argument names (e.g., `CreateDistributionRequestRequestTypeDef` -> `CreateDistributionRequestTypeDef`) and moving `Extra` postfixes. This might require updating type hint declarations for specific S3 operations if your code relies on these specific `TypeDef` names.
- gotcha This package (`types-boto3-s3`) provides *only* type annotations. The actual `boto3` library must be installed separately for your code to run correctly. This package is a development dependency, not a runtime dependency.
- gotcha For optimal performance and to avoid unnecessary runtime dependencies, always enclose `types-boto3-s3` imports within an `if TYPE_CHECKING:` block. This ensures that the type stubs are only processed by static analysis tools and not imported at runtime.
- gotcha PyCharm users may experience slow performance or high CPU usage due to how PyCharm handles `Literal` overloads with `types-boto3` packages. In such cases, using `types-boto3-lite` (e.g., `pip install types-boto3-lite[s3]`) is recommended, which offers a more RAM-friendly alternative by not providing `session.client`/`resource` overloads, requiring explicit type annotations.
Install
-
pip install types-boto3-s3 boto3
Imports
- S3Client
from types_boto3_s3.client import S3Client
- S3ServiceResource
from types_boto3_s3.service_resource import S3ServiceResource
- ListBucketsOutputTypeDef
from types_boto3_s3.type_defs import ListBucketsOutputTypeDef
Quickstart
import boto3
from typing import TYPE_CHECKING
# Only import type stubs during type checking to avoid runtime dependency
if TYPE_CHECKING:
from types_boto3_s3.client import S3Client
from types_boto3_s3.type_defs import ListBucketsOutputTypeDef
def list_s3_bucket_names() -> list[str]:
# The actual boto3 client is used at runtime
s3_client: 'S3Client' = boto3.client(
"s3",
aws_access_key_id=os.environ.get('AWS_ACCESS_KEY_ID', ''),
aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY', ''),
region_name=os.environ.get('AWS_REGION', 'us-east-1')
)
print("Listing S3 buckets...")
response: 'ListBucketsOutputTypeDef' = s3_client.list_buckets()
bucket_names = [bucket['Name'] for bucket in response.get('Buckets', [])]
return bucket_names
if __name__ == "__main__":
import os
# Ensure AWS credentials and region are set in environment variables for a runnable example
# e.g., export AWS_ACCESS_KEY_ID='YOUR_KEY' AWS_SECRET_ACCESS_KEY='YOUR_SECRET' AWS_REGION='us-east-1'
if not all(os.environ.get(k) for k in ['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_REGION']):
print("Please set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_REGION environment variables.")
else:
try:
buckets = list_s3_bucket_names()
if buckets:
print(f"Found buckets: {', '.join(buckets)}")
else:
print("No S3 buckets found.")
except Exception as e:
print(f"An error occurred: {e}")