Re: [boto-users] Cannot get put_item to work with DynamoDB2

310 views
Skip to first unread message

Victor Trac

unread,
Dec 19, 2014, 11:16:32 AM12/19/14
to boto-users
Try using the higher level API with a Table object: http://boto.readthedocs.org/en/latest/dynamodb2_tut.html#creating-a-new-item.

--
Victor Trac  |  victortrac.com  |  twitter.com/victortrac 

On Fri, Dec 19, 2014 at 1:10 AM, Keith Adler <nissan...@gmail.com> wrote:
The following code results in an error on put_item.  I have a .boto file and the connection works fine.  Unfortunately this always gets this error on put_item:

boto.exception.JSONResponseError: JSONResponseError: 400 Bad Request
{'Message': 'Expected null', '__type': 'com.amazon.coral.service#SerializationException'}

I have already created the table and the primary key type is Hash and called id.  I have given the necessary permissions.

#!/usr/bin/env python3
""" Game server.

Usage:
python3 main.py <debug>
"""


# Imports
import boto
from boto.dynamodb2.table import Table
from boto.dynamodb2.layer1 import DynamoDBConnection
from datetime import datetime
from flask import *
from random import shuffle
import sys
import uuid


# Constants
TOKEN = 'Some Token'
AWS_REGION = 'us-east-2'


# Modules
app = Flask(__name__)
conn = boto.dynamodb2.connect_to_region(region_name=AWS_REGION)
games = Table('games', connection=conn)


@app.route('/game-server/api/v1.0/create_new_game/<token>/<player_1>/<player_2>/<rules>',
methods=['GET'])
def create_new_game(token, player_1, player_2, rules):
"""Creates a new game from scratch

:param token: Unique value that verifies the caller is valid
:param player_1: Name of the first player
:param player_2: Name of the second player
:param rules: String representing the rules to be used in the game
:return: UUID representing the game id that has been created
"""

# Create game
game_id = str(uuid.uuid4())
item_data = {
'id': game_id,
'created': str(datetime.utcnow()),
}
conn.put_item('games', item=item_data)

return game_id


# Main entry point
if __name__ == "__main__":
app.run(debug=True, port=8192)

--
You received this message because you are subscribed to the Google Groups "boto-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to boto-users+...@googlegroups.com.
To post to this group, send email to boto-...@googlegroups.com.
Visit this group at http://groups.google.com/group/boto-users.
For more options, visit https://groups.google.com/d/optout.

Danny Cosson

unread,
Feb 11, 2015, 2:10:47 PM2/11/15
to boto-...@googlegroups.com
I'm having this issue as well. I get the same error with both put_item and update_item on a boto.dynamodb2.layer1 connection object.  I need to do an update_item to append to a string set and as far as I can tell this only exists in layer 1 so I can't use the higher level api.

I am surprised to see the only response to this issue was to use a different API though - does this mean the layer1 api in boto is not expected to work? I haven't found anywhere that says it's deprecated.

Thanks,
Danny

Danny Cosson

unread,
Feb 11, 2015, 2:49:45 PM2/11/15
to boto-...@googlegroups.com
Ok, figured out the issue. The Item and Key fields have to be wrapped with the data types in the layer1 API so OP's request should be:

item_data = {
'id': {'S': game_id},
'created': {'S': str(datetime.utcnow())},
}

--
You received this message because you are subscribed to a topic in the Google Groups "boto-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/boto-users/ITlD-4Qsf9c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to boto-users+...@googlegroups.com.

To post to this group, send email to boto-...@googlegroups.com.
Visit this group at http://groups.google.com/group/boto-users.
For more options, visit https://groups.google.com/d/optout.



--
Danny

Danny Cosson

unread,
Feb 11, 2015, 2:54:02 PM2/11/15
to boto-...@googlegroups.com
It's also worth nothing that somehow dynamodb local doesn't require this formatting so the original code example works locally, you can put the item and then query and get it back.  But then when you deploy to aws you'll have a bad time.

On Wed, Feb 11, 2015 at 2:49 PM, Danny Cosson <dco...@gmail.com> wrote:
Ok, figured out the issue. The Item and Key fields have to be wrapped with the data types in the layer1 API so OP's request should be:

item_data = {
'id': {'S': game_id},
'created': {'S': str(datetime.utcnow())},
}
--
Danny



--
Danny
Reply all
Reply to author
Forward
0 new messages