types-aiobotocore-s3 Type Stubs

3.4.0 · active · verified Thu Apr 09

types-aiobotocore-s3 provides comprehensive type annotations for `aiobotocore`'s S3 service, enabling static type checking with tools like MyPy. It is part of the `mypy-boto3-builder` project, which generates stubs for all `aiobotocore` services. The library follows a rapid release cadence, often aligning with `aiobotocore` and `mypy-boto3-builder` updates, with its current version being 3.4.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `aiobotocore` with `types-aiobotocore-s3` for type-hinted interaction with Amazon S3. Once `types-aiobotocore-s3` is installed, `aiobotocore` client objects automatically gain type information. For explicit annotations, the `S3Client` type can be imported from `types_aiobotocore_s3.client` within a `TYPE_CHECKING` block to avoid runtime dependency. The example lists all S3 buckets in a specified region.

import asyncio
from os import environ
from typing import TYPE_CHECKING

# Only import S3Client for type checking, not for runtime
if TYPE_CHECKING:
    from types_aiobotocore_s3.client import S3Client

from aiobotocore.session import get_session

async def list_s3_buckets():
    session = get_session()
    async with session.create_client("s3", region_name="us-east-1") as client:
        # The 'client' object is automatically typed as S3Client
        # if types-aiobotocore-s3 is installed.
        # Explicit annotation for clarity:
        s3_client: S3Client = client
        response = await s3_client.list_buckets()
        for bucket in response.get("Buckets", []):
            print(f"Bucket Name: {bucket['Name']}")

        # Example of type-checked operation
        # await s3_client.non_existent_method() # MyPy would flag this

if __name__ == "__main__":
    # Set dummy credentials for quickstart if not already set
    environ.setdefault('AWS_ACCESS_KEY_ID', 'testing')
    environ.setdefault('AWS_SECRET_ACCESS_KEY', 'testing')
    environ.setdefault('AWS_SECURITY_TOKEN', 'testing')
    environ.setdefault('AWS_DEFAULT_REGION', 'us-east-1')

    asyncio.run(list_s3_buckets())

view raw JSON →