Boto SDK 的新功能。我有以下单元测试要检查:
def test_dynamo():
conn = DynamoDBConnection(
host='localhost',
port=8000,
aws_secret_access_key='anything',
is_secure=False)
test_table = Table.create('table_for_test',schema=[HashKey('identifier',data_type=STRING)],connection=conn)
# test_table = Table('test',connection=conn)
unique_key = lskinesis_util.generate_unique_id()
time.sleep(5)
payload = {"identifier":unique_key,"stamp":"30/3/2014 14:39", "type":"testing-start","level":"info","source":"test-runner","user":{"id":5060342395,"first_name":"Alex"}}
encoder = json.JSONEncoder()
ejson = encoder.encode(payload)
test_table.put_item(data=ejson)
time.sleep(5)
from_db = test_table.get_item(identifier=unique_key)
assert from_db == ejson
Table.delete()
当我运行它时,出现以下错误:
E AttributeError: 'str' object has no attribute 'keys'
你能告诉我我在这里缺少什么吗?提前致谢。
最佳答案
Dynamodb 只能存储普通字典:
根据文件,
只需交给它一个数据字典,它就会在服务器端创建该项目。该字典应该相对平坦(因为您可以嵌套在其他字典中)并且必须包含架构中使用的键。
为了存储以下字典,
s = {'username':'anu', 'key':'1', 'value':'1', 'user_details':{'name':'Anu','age':'1'}}
d = base64.b64encode(json.dumps(s).encode('ascii'))
users.put_item(data={'key':'3','value':d})
即,您可以存储base64编码的数据。
关于python - Boto Dynamo 数据库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22742841/