aioboto3
aioboto3 is an asynchronous wrapper for boto3, the Amazon Web Services (AWS) SDK for Python. It allows developers to interact with AWS services using Python's `async/await` syntax, leveraging the `aiobotocore` library for its asynchronous backend. This enables non-blocking I/O operations with AWS, ideal for modern async applications. The current version is 15.5.0, with frequent releases often aligned with updates to its underlying dependencies.
Warnings
- breaking Breaking Change in v9: Direct calls to `aioboto3.resource()` and `aioboto3.client()` are no longer available. You must first create a `Session` object and then use `session.client()` or `session.resource()`.
- breaking Breaking Change in v9/v8.0.0+: `client` and `resource` methods (obtained from a session) must now be used as asynchronous context managers (`async with`). This change aligns with `aiobotocore` 1.0.1+ behavior.
- breaking Breaking Change in v11: The `S3Transfer` configuration passed into S3 `upload_file` and `download_file` methods has been updated to match `boto3`'s behavior, which might require adjustments if you were passing custom transfer configurations.
- gotcha Service-specific resource objects (e.g., `s3.Bucket`, `dynamo_resource.Table`) must be `await`ed when instantiated, as their creation is an asynchronous operation.
- gotcha For long-running processes like web servers, directly using `async with session.client(...)` per request can introduce overhead. For persistent clients, an `AsyncExitStack` or similar pattern is recommended to manage the context manager lifecycle.
Install
-
pip install aioboto3 -
pip install aioboto3[s3cse]
Imports
- Session
from aioboto3 import Session
Quickstart
import asyncio
import os
from aioboto3 import Session
from boto3.dynamodb.conditions import Key
async def main():
aws_region = os.environ.get('AWS_DEFAULT_REGION', 'us-east-1')
session = Session()
async with session.resource('dynamodb', region_name=aws_region) as dynamodb_resource:
table_name = 'my_async_table'
# Check if table exists, create if not (simplified for quickstart)
try:
await dynamodb_resource.Table(table_name).wait_until_exists()
print(f"Table '{table_name}' already exists.")
except Exception:
print(f"Creating table '{table_name}'...")
table = await dynamodb_resource.create_table(
TableName=table_name,
KeySchema=[
{'AttributeName': 'pk', 'KeyType': 'HASH'}
],
AttributeDefinitions=[
{'AttributeName': 'pk', 'AttributeType': 'S'}
],
BillingMode='PAY_PER_REQUEST'
)
await table.wait_until_exists()
print(f"Table '{table_name}' created successfully.")
table = await dynamodb_resource.Table(table_name)
table = await dynamodb_resource.Table(table_name)
print("Putting item...")
await table.put_item(
Item={'pk': 'test1', 'data': 'async_data_value'}
)
print("Item put.")
print("Querying item...")
result = await table.query(
KeyConditionExpression=Key('pk').eq('test1')
)
print("Query result:", result['Items'])
# Clean up (optional, for demonstration)
print(f"Deleting table '{table_name}'...")
await table.delete()
await table.wait_until_not_exists()
print(f"Table '{table_name}' deleted.")
if __name__ == '__main__':
asyncio.run(main())