DynamoDB is a regional service
Add boto3.s3` type stubs
pip install "boto3-stubs[dynamodb]"DynamoDB Compulsory
- Compulsory to create DynamoDB:
- TableName
- KeySchema
- Partition key (HASH)
- Sort key (Range)
- AttributeDefinition
- AttributeType:
- S: String
- N: Number
- B: Binary
- AttributeType:
- Capacity model
BillingMode="PAY_PER_REQUEST", on-Demand- Provisioned capacity,
- set Read Capacity Units
- set Write Capacity Units
Create Table
When creating a table, can only definite the key attributes used in the key schema.
def create_dynamoDB(tablename, region):
dynamoDB = boto3.client("dynamodb", region_name=region)
if check_table_exist(tablename, regions=region):
print(f"--- Table:{tablename} exists ---")
return
try:
dynamoDB.create_table(
TableName=tablename,
BillingMode="PAY_PER_REQUEST",
AttributeDefinitions=[
{"AttributeName": "bucket_name", "AttributeType": "S"},
{"AttributeName": "timestamp", "AttributeType":"N"}
],
KeySchema=[
{"AttributeName": "bucket_name", "KeyType": "HASH"},
{"AttributeName": "timestamp", "KeyType": "RANGE"}
]
)
print(f"--- Created Table:{tablename} ---")
except ClientError as e:
print(e)Check exist Table
def check_table_exist(table, regions):
dynamoDB = boto3.client("dynamodb", region_name=region)
try:
dynamoDB.describe_table(TableName=tablename)
return True
except ClientError as e:
code = e.response.get("Error", {}).get("Code")
if code == "404":
return False # not exist
if code == "403":
print(f"--- AccessDenied ----")