auhuman
auhuman

Reputation: 972

dynamodb - boto3 client works resource throws error

When I use the client to get_item from a table using Key it works fine. If I try the same with Resource I get error. Whats going wrong here

>>> ddbr = boto3.resource('dynamodb')
>>> table = ddbr.Table('employees')
>>> table.get_item(Key={'EmpId': {'S': '13456789ABC'}})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/qwert/miniconda2/envs/pyenv36/lib/python3.6/site-packages/boto3/resources/factory.py", line 520, in do_action
    response = action(self, *args, **kwargs)
  File "/Users/qwert/miniconda2/envs/pyenv36/lib/python3.6/site-packages/boto3/resources/action.py", line 83, in __call__
    response = getattr(parent.meta.client, operation_name)(**params)
  File "/Users/qwert/miniconda2/envs/pyenv36/lib/python3.6/site-packages/botocore/client.py", line 312, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/Users/qwert/miniconda2/envs/pyenv36/lib/python3.6/site-packages/botocore/client.py", line 601, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema

Upvotes: 0

Views: 1434

Answers (2)

jarmod
jarmod

Reputation: 78850

Boto3 provides two service abstractions:

  1. Client (low-level)
  2. Resource (high-level, object-oriented abstraction on top of Client)

And there are two corresponding DynamoDB get_item methods:

  1. DynamoDB Client has a get_item() method
  2. DynamoDB Table (a Resource) has a get_item() method

If you read the documentation for each method you will see that they expect the item key to be provided in two different ways (in my example I'm assuming it's a string):

  1. Client: Key = { "column" : { "S" : "value" } }
  2. Resource: Key = { "column" : "value" }

There's a good Introduction to Version 3 of the AWS SDK for Python video.

Upvotes: 1

Nath
Nath

Reputation: 970

No need to declare the type in dynamo Json format; boto does that for you. If your table’s partition key is named ‘EmpId’ and is of type string your get item call should look like

table.get_item(Key={'EmpId':'13456789ABC'})

Upvotes: 2

Related Questions