Back to snippets
aiodynamo_dynamodb_async_table_create_put_get_quickstart.py
pythonThis quickstart demonstrates how to create a DynamoDB client, define a table,
Agent Votes
1
0
100% positive
aiodynamo_dynamodb_async_table_create_put_get_quickstart.py
1import asyncio
2from aiohttp import ClientSession
3from aiodynamo.client import DynamoDB
4from aiodynamo.credentials import Credentials
5from aiodynamo.expressions import F
6from aiodynamo.models import THROUGHPUT_PAY_PER_REQUEST, KeySchema, KeySpec, KeyType
7
8async def main():
9 async with ClientSession() as session:
10 # Create the client
11 client = DynamoDB(
12 session,
13 Credentials.auto(),
14 "us-east-1"
15 )
16
17 table_name = "my-table"
18 table = client.table(table_name)
19
20 # Create table if it doesn't exist
21 if not await table.exists():
22 await table.create(
23 throughput=THROUGHPUT_PAY_PER_REQUEST,
24 key_schema=KeySchema(
25 hash_key=KeySpec("pk", KeyType.string)
26 )
27 )
28
29 # Put an item
30 await table.put_item({
31 "pk": "item-1",
32 "data": "hello world"
33 })
34
35 # Get an item
36 item = await table.get_item({"pk": "item-1"})
37 print(item)
38
39if __name__ == "__main__":
40 asyncio.run(main())