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)


2016-06-26 07:57:22.517000-04:00

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)


0

In [24]:
table.delete()


Out[24]:
{'ResponseMetadata': {'HTTPHeaders': {'content-length': '570',
   'content-type': 'application/x-amz-json-1.0',
   'server': 'Jetty(8.1.12.v20130726)',
   'x-amz-crc32': '700433039',
   'x-amzn-requestid': 'e28f2830-c47e-40f5-b7a3-3a6ef111e0b0'},
  'HTTPStatusCode': 200,
  'RequestId': 'e28f2830-c47e-40f5-b7a3-3a6ef111e0b0'},
 u'TableDescription': {u'AttributeDefinitions': [{u'AttributeName': u'media',
    u'AttributeType': u'S'},
   {u'AttributeName': u'barcode', u'AttributeType': u'S'}],
  u'CreationDateTime': datetime.datetime(2016, 6, 26, 7, 56, 34, 447000, tzinfo=tzlocal()),
  u'ItemCount': 0,
  u'KeySchema': [{u'AttributeName': u'media', u'KeyType': u'HASH'},
   {u'AttributeName': u'barcode', u'KeyType': u'RANGE'}],
  u'ProvisionedThroughput': {u'LastDecreaseDateTime': datetime.datetime(1969, 12, 31, 19, 0, tzinfo=tzlocal()),
   u'LastIncreaseDateTime': datetime.datetime(1969, 12, 31, 19, 0, tzinfo=tzlocal()),
   u'NumberOfDecreasesToday': 0,
   u'ReadCapacityUnits': 5,
   u'WriteCapacityUnits': 5},
  u'TableArn': u'arn:aws:dynamodb:ddblocal:000000000000:table/foo',
  u'TableName': u'foo',
  u'TableSizeBytes': 0,
  u'TableStatus': u'ACTIVE'}}

In [29]:
table.put_item(
    Item={
        'media': 'janedoe',
        'barcode': 'Jane',
    }
)


Out[29]:
{'ResponseMetadata': {'HTTPHeaders': {'content-length': '2',
   'content-type': 'application/x-amz-json-1.0',
   'server': 'Jetty(8.1.12.v20130726)',
   'x-amz-crc32': '2745614147',
   'x-amzn-requestid': 'c147f686-82e4-4a88-9539-bdb1e60a3aeb'},
  'HTTPStatusCode': 200,
  'RequestId': 'c147f686-82e4-4a88-9539-bdb1e60a3aeb'}}

In [31]:
response = table.get_item(
Key={
'media': 'janedoe',
'barcode': 'Jane'
}
)
item = response['Item']
print(item)


{u'media': u'janedoe', u'barcode': u'Jane'}

In [32]:
item['barcode'] = 'snarf'
table.put_item(Item=item)


Out[32]:
{'ResponseMetadata': {'HTTPHeaders': {'content-length': '2',
   'content-type': 'application/x-amz-json-1.0',
   'server': 'Jetty(8.1.12.v20130726)',
   'x-amz-crc32': '2745614147',
   'x-amzn-requestid': '2034248f-bbb4-420b-a2dc-cd966fe1cbfd'},
  'HTTPStatusCode': 200,
  'RequestId': '2034248f-bbb4-420b-a2dc-cd966fe1cbfd'}}

In [33]:
response = table.get_item(
Key={
'media': 'janedoe',
'barcode': 'snarf'
}
)
item = response['Item']
print(item)


{u'media': u'janedoe', u'barcode': u'snarf'}

In [35]:
table.delete_item(
Key={
'media': 'janedoe',
'barcode': 'snarf'
}
)


Out[35]:
{'ResponseMetadata': {'HTTPHeaders': {'content-length': '2',
   'content-type': 'application/x-amz-json-1.0',
   'server': 'Jetty(8.1.12.v20130726)',
   'x-amz-crc32': '2745614147',
   'x-amzn-requestid': 'eb2648c6-6ab2-4016-95d4-e263a5418c02'},
  'HTTPStatusCode': 200,
  'RequestId': 'eb2648c6-6ab2-4016-95d4-e263a5418c02'}}

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)