GetItem is a DynamoDB read operation that fetches one single item using its primary key.

  • If your table has Partition Key (PK) only, you provide that one key.

DynamoDB

  • ProjectionExpression
    • tells DynamoDB which attributes (fields) to return.
  • ExpressionAttributeNames
    • ExpressionAttributeNames={'n': 'Name'}
    • some attribute names can’t be used directly
  • DynamoDB has two read consistency modes:
    • Default: Eventually consistent
      • may briefly return an older value right after an update
    • Strongly consistent: ConsistentRead=True
      • guarantees you see the latest written value (after a successful write)
def get_item_max(tablename, max_key):
    dynamodb = boto3.client("dynamodb")
    resp = dynamodb.get_item(
        TableName=tablename,
        Key={
            "bucket_name": {"S": max_key},
            "timestamp": {"N": "0"}
        },
        ProjectionExpression="total_size_bytes",
        ConsistentRead=True
    )
    item = resp.get("Item")
    if not item:
        return 0
    return int(item["total_size_bytes"]["N"])