In [37]:
import boto3
from boto3.dynamodb.conditions import Key, Attr
ddb = boto3.resource('dynamodb',
endpoint_url='http://localhost:8000',
region_name='us-west-2',
aws_access_key_id='AKIAIO5FODNN7EXAMPLE',
aws_secret_access_key='ABCDEF+c2L7yXeGvUyrPgYsDnWRRC1AYEXAMPLE')
In [28]:
# Instantiate a table resource object without actually
# creating a DynamoDB table. Note that the attributes of this table
# are lazy-loaded: a request is not made nor are the attribute
# values populated until the attributes
# on the table resource are accessed or its load() method is called.
table = ddb.Table('foo')
# Print out some data about the table.
# This will cause a request to be made to DynamoDB and its attribute
# values will be set based on the response.
print(table.creation_date_time)
In [25]:
table = ddb.create_table(
TableName='foo',
KeySchema=[
{
'AttributeName': 'media',
'KeyType': 'HASH'
},
{
'AttributeName': 'barcode',
'KeyType': 'RANGE'
}
],
AttributeDefinitions=[
{
'AttributeName': 'media',
'AttributeType': 'S'
},
{
'AttributeName': 'barcode',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
}
)
# Wait until the table exists.
table.meta.client.get_waiter('table_exists').wait(TableName='users')
# Print out some data about the table.
print(table.item_count)
In [24]:
table.delete()
Out[24]:
In [29]:
table.put_item(
Item={
'media': 'janedoe',
'barcode': 'Jane',
}
)
Out[29]:
In [31]:
response = table.get_item(
Key={
'media': 'janedoe',
'barcode': 'Jane'
}
)
item = response['Item']
print(item)
In [32]:
item['barcode'] = 'snarf'
table.put_item(Item=item)
Out[32]:
In [33]:
response = table.get_item(
Key={
'media': 'janedoe',
'barcode': 'snarf'
}
)
item = response['Item']
print(item)
In [35]:
table.delete_item(
Key={
'media': 'janedoe',
'barcode': 'snarf'
}
)
Out[35]:
In [38]:
response = table.query(
KeyConditionExpression=Key('media').eq('johndoe')
)
items = response['Items']
print(items)
In [ ]:
response = table.scan(
FilterExpression=Attr('address.state').eq('CA')
)
items = response['Items']
print(items)