How to instantiate clients and resources in aioboto3
mainIn aioboto3, you must use aioboto3.Session() to create a session, and then instantiate clients or resources using async context managers. You can no longer call aioboto3.client() or aioboto3.resource() directly. This pattern ensures that the session lifecycle is correctly managed within the event loop.
To access a resource, use the async with session.resource(...) as resource_name: syntax. Note that accessing properties on resource objects (like an S3 Bucket) is also asynchronous; these properties are coroutines that load metadata on the first await and cache it thereafter.
async def main():
session = aioboto3.Session()
async with session.resource("s3") as s3:
bucket = await s3.Bucket('mybucket')
async for s3_object in bucket.objects.all():
print(s3_object)