Skip to content

Examples

Index > S3 > Examples

Auto-generated documentation for S3 type annotations stubs module mypy-boto3-s3.

Client

Implicit type annotations

Can be used with boto3-stubs[s3] package installed.

Write your S3 code as usual, type checking and code completion should work out of the box.

Client usage example
from boto3.session import Session


session = Session()

client = session.client("s3")  # (1)
result = client.abort_multipart_upload()  # (2)
  1. client: S3Client
  2. result: AbortMultipartUploadOutputTypeDef
Paginator usage example
from boto3.session import Session


session = Session()
client = session.client("s3")  # (1)

paginator = client.get_paginator("list_multipart_uploads")  # (2)
for item in paginator.paginate(...):
    print(item)  # (3)
  1. client: S3Client
  2. paginator: ListMultipartUploadsPaginator
  3. item: ListMultipartUploadsOutputTypeDef
Waiter usage example
from boto3.session import Session


session = Session()
client = session.client("s3")  # (1)

waiter = client.get_waiter("bucket_exists")  # (2)
waiter.wait()
  1. client: S3Client
  2. waiter: BucketExistsWaiter

Explicit type annotations

With boto3-stubs-lite[s3] or a standalone mypy_boto3_s3 package, you have to explicitly specify client: S3Client type annotation.

All other type annotations are optional, as types should be discovered automatically. However, these type annotations can be helpful in your functions and methods.

Client usage example
from boto3.session import Session

from mypy_boto3_s3.client import S3Client
from mypy_boto3_s3.type_defs import AbortMultipartUploadOutputTypeDef
from mypy_boto3_s3.type_defs import AbortMultipartUploadRequestRequestTypeDef


session = Session()

client: S3Client = session.client("s3")

kwargs: AbortMultipartUploadRequestRequestTypeDef = {...}
result: AbortMultipartUploadOutputTypeDef = client.abort_multipart_upload(**kwargs)
Paginator usage example
from boto3.session import Session

from mypy_boto3_s3.client import S3Client
from mypy_boto3_s3.paginator import ListMultipartUploadsPaginator
from mypy_boto3_s3.type_defs import ListMultipartUploadsOutputTypeDef


session = Session()
client: S3Client = session.client("s3")

paginator: ListMultipartUploadsPaginator = client.get_paginator("list_multipart_uploads")
for item in paginator.paginate(...):
    item: ListMultipartUploadsOutputTypeDef
    print(item)
Waiter usage example
from boto3.session import Session

from mypy_boto3_s3.client import S3Client
from mypy_boto3_s3.waiter import BucketExistsWaiter

session = Session()
client: S3Client = session.client("s3")

waiter: BucketExistsWaiter = client.get_waiter("bucket_exists")
waiter.wait()

Service Resource

Implicit type annotations

Can be used with boto3-stubs[s3] package installed.

ServiceResource usage example
from boto3.session import Session


session = Session()

resource = session.resource("s3")  # (1)
result = resource.Bucket()  # (2)
  1. resource: S3ServiceResource
  2. result:
Collection usage example
from boto3.session import Session


session = Session()
resource = session.resource("s3")  # (1)

collection = resource.buckets  # (2)
for item in collection:
    print(item)  # (3)
  1. resource: S3ServiceResource
  2. collection: S3ServiceResource
  3. item: Bucket

Explicit type annotations

With boto3-stubs-lite[s3] or a standalone mypy_boto3_s3 package, you have to explicitly specify resource: S3ServiceResource type annotation.

All other type annotations are optional, as types should be discovered automatically. However, these type annotations can be helpful in your functions and methods.

ServiceResource usage example
from boto3.session import Session

from mypy_boto3_s3.service_resource import S3ServiceResource
from mypy_boto3_s3.service_resource import Bucket


session = Session()

resource: S3ServiceResource = session.resource("s3")
result: Bucket = resource.Bucket()
Collection usage example
from boto3.session import Session

from mypy_boto3_s3.service_resource import S3ServiceResource
from mypy_boto3_s3.service_resource import ServiceResourceBucketsCollection
from mypy_boto3_s3.service_resource import Bucket


session = Session()

resource: S3ServiceResource = session.resource("s3")

collection: ServiceResourceBucketsCollection = resource.buckets
for item in collection:
    item: Bucket
    print(item)