Skip to content

Instantly share code, notes, and snippets.

@xlbruce
Created March 19, 2019 13:52
Show Gist options
  • Save xlbruce/f274215708e9a69b183358babf0c336c to your computer and use it in GitHub Desktop.
Save xlbruce/f274215708e9a69b183358babf0c336c to your computer and use it in GitHub Desktop.
AWS SimpleDB mini crud
import boto3
sdb = boto3.client('sdb')
domain_name = 'sdb_test'
def make_item(item_name, attributes):
return {
'ItemName': item_name,
'Attributes': [{'Name': k, 'Value': v, 'Replace': True} for k, v in attributes.items()]
}
def get_item(item_name):
response = sdb.get_attributes(DomainName=domain_name, ItemName=item_name)
try:
item = {attr['Name']:attr['Value'] for attr in response['Attributes']}
except KeyError as e:
raise ValueError('[{}] not found'.format(item_name))
item['ItemName'] = item_name
return item
def delete_item(item_name):
response = sdb.delete_attributes(DomainName=domain_name, ItemName=item_name)
return response['ResponseMetadata']['HTTPStatusCode'] is 200
def put_item(item):
response = sdb.put_attributes(DomainName=domain_name, **item)
return response['ResponseMetadata']['HTTPStatusCode'] is 200
'''
Example:
item = make_item('my_item', {'attr1':'value1'})
put_item(item)
print(get_item('my_item'))
delete_item('item_name')
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment