作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 Boto3/DynamoDB 时遇到困难 BatchGetItem 手术。我将不胜感激任何帮助或指导!如果这是一个新手问题,我对 python/aws 还很陌生,所以很抱歉。
当我执行操作时,我收到此错误:
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the BatchGetItem operation: The provided key element does not match the schema
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',
}
}
)
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:\Users\Henry Miller\PycharmProjects\bioinformatics_webapp\venv\lib\site-packages\boto3\resources\factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "C:\Users\Henry Miller\PycharmProjects\bioinformatics_webapp\venv\lib\site-packages\boto3\resources\action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
File "C:\Users\Henry Miller\PycharmProjects\bioinformatics_webapp\venv\lib\site-packages\botocore\client.py", line 314, in _api_call
return self._make_api_call(operation_name, kwargs)
File "C:\Users\Henry Miller\PycharmProjects\bioinformatics_webapp\venv\lib\site-packages\botocore\client.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
最佳答案
在这里回答我自己的问题......但事实证明你需要使用 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',
}
}
)
"C:\Users\Henry Miller\PycharmProjects\bioinformatics_webapp\venv\Scripts\python.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
关于python-3.x - BatchGetItem 操作 : "The provided key element does not match the schema" 上的 DynamoDB 和 Boto3 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51604162/
我是一名优秀的程序员,十分优秀!