Question

New to Boto SDK. I have the following unit test to check:

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()

When I run it I get the following error:

E       AttributeError: 'str' object has no attribute 'keys'

Can you please let me know what am I missing here? Thanks in advance.

Was it helpful?

Solution 2

Ok I figured it out. There is no need to do the following:

encoder = json.JSONEncoder()

ejson = encoder.encode(payload)

However I got another problem now:

E           TypeError: Unsupported type "<type 'dict'>" for value "{'first_name': 'Alex', 'id': 5060342395}"

Any idea why the object user in the above payload is being treated as a dictionary?

Thanks

OTHER TIPS

Dynamodb can store only plain dictionary:

As per the document,

Simply hand it a dictionary of data & it will create the item on the server side. This dictionary should be relatively flat (as you can nest in other dictionaries) & must contain the keys used in the schema.

In-order to store the following dict,

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})

ie, you can store base64 encoded data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top