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.
Common errors
-
ModuleNotFoundError: No module named 'aioboto3'
cause The 'aioboto3' library is not installed in the Python environment.fixpip install aioboto3 -
AttributeError: 'ResourceCreatorContext' object has no attribute 'Table'
cause The 'resource' method in 'aioboto3' is asynchronous and needs to be awaited.fixdynamodb = await aioboto3.Session().resource('dynamodb') -
AttributeError: module 'aiobotocore' has no attribute 'AioSession'
cause The 'AioSession' attribute is not present in the 'aiobotocore' module, possibly due to version incompatibility.fixEnsure compatible versions of 'aioboto3' and 'aiobotocore' are installed, or downgrade 'aioboto3' to a version where 'AioSession' is available. -
AttributeError: 'ClientCreatorContext' object has no attribute 'invoke_endpoint'
cause The 'client' method in 'aioboto3' is asynchronous and needs to be awaited.fixsagemaker_client = await aioboto3.Session().client('sagemaker-runtime') -
RuntimeWarning: coroutine '...' was never awaited
cause An asynchronous function (coroutine) was called, but its execution was not properly initiated using the `await` keyword or by `asyncio.run()`, meaning the coroutine object was created but never executed.fixEnsure all calls to `async` functions within an `async` context are prefixed with `await`. If calling from synchronous code, wrap the coroutine call with `asyncio.run()`.
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.
- gotcha aioboto3 could not find AWS credentials. This often manifests as `botocore.exceptions.NoCredentialsError: Unable to locate credentials` when attempting to interact with AWS services.
- gotcha Unable to locate AWS credentials. Ensure your AWS credentials are configured via environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY), shared credentials file (~/.aws/credentials), or IAM roles/profiles.
Install
-
pip install aioboto3 -
pip install aioboto3[s3cse]
Imports
- Session
import aioboto3; client = aioboto3.client('s3')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())