DynamoDB and Boto3 error on BatchGetItem operation: “The provided key element does not match the schema”

Clash Royale CLAN TAG#URR8PPPDynamoDB and Boto3 error on BatchGetItem operation: “The provided key element does not match the schema”
I am having difficulty with the Boto3/DynamoDB BatchGetItem operation. I would greatly appreciate any help or guidance! I'm pretty new to python/aws so sorry in advanced if this is a novice question.
When I perform the operation, I get this error:
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the BatchGetItem operation: The provided key element does not match the schema
Here is my code:
import boto3
dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
response = dynamodb.batch_get_item(
RequestItems={
'test': {
'Keys': [
{
'item_ID': {
'S': '1'
}
},
{
'item_ID': {
'S': '2'
}
}
],
'ProjectionExpression': 'item_ID, color',
}
}
)
This is a screen-cap of the items in the table.
This is a screen-cap of the table details, showing the partition key is 'item_ID' and that it's a 'string'
Here is the fulll error message:
Traceback (most recent call last):
File "C:/Users/Henry Miller/PycharmProjects/bioinformatics_webapp/get_items.py", line 18, in <module>
'ProjectionExpression': 'item_ID, color',
File "C:UsersHenry MillerPycharmProjectsbioinformatics_webappvenvlibsite-packagesboto3resourcesfactory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "C:UsersHenry MillerPycharmProjectsbioinformatics_webappvenvlibsite-packagesboto3resourcesaction.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
File "C:UsersHenry MillerPycharmProjectsbioinformatics_webappvenvlibsite-packagesbotocoreclient.py", line 314, in _api_call
return self._make_api_call(operation_name, kwargs)
File "C:UsersHenry MillerPycharmProjectsbioinformatics_webappvenvlibsite-packagesbotocoreclient.py", line 612, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the BatchGetItem operation: The provided key element does not match the schema
1 Answer
1
Answering my own question here... but it turns out that you need to use boto3.client instead of boto3.resouce. Here is the updated code which works:
boto3.client
boto3.resouce
import boto3
dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
client = boto3.client('dynamodb', region_name='us-west-2')
response = client.batch_get_item(
RequestItems={
'test': {
'Keys': [
{
'item_ID': {
'S': '1'
}
},
{
'item_ID': {
'S': '2'
}
}
],
'ProjectionExpression': 'item_ID, color',
}
}
)
And here was the response:
"C:UsersHenry MillerPycharmProjectsbioinformatics_webappvenvScriptspython.exe" "C:/Users/Henry Miller/PycharmProjects/bioinformatics_webapp/get_items.py"
{'Responses': {'test': [{'item_ID': {'S': '1'}, 'color': {'S': 'red'}}, {'item_ID': {'S': '2'}, 'color': {'S': 'blue'}}]}, 'UnprocessedKeys': {}, 'ResponseMetadata': {'RequestId': 'BAH1SHCBHOMRJMJ5AHE7VRTON3VV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Tue, 31 Jul 2018 04:15:13 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '130', 'connection': 'keep-alive', 'x-amzn-requestid': 'BAH1SHCBHOMRJMJ5AHE7VRTON3VV4KQNSO5AEMVJF66Q9ASUAAJG', 'x-amz-crc32': '1917096114'}, 'RetryAttempts': 0}}
Process finished with exit code 0
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.