gpt4 book ai didi

python - 无法使用 python 从 AWS dynamodb 获取项目?

转载 作者:太空狗 更新时间:2023-10-30 01:48:03 28 4
gpt4 key购买 nike

我是 dynamodb 的新手,试图从 dynamodb 获取数据。

This is my table with "topic" as a primary hash key

我的python代码

import boto3 
from boto3 import dynamodb

from boto3.session import Session

from boto3.dynamodb.conditions import Key, Attr


dynamodb_session = Session(aws_access_key_id='XXXXXXXXXXXXXXX',
aws_secret_access_key='XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
region_name='us-east-1')

dynamodb = dynamodb_session.resource('dynamodb')

table=dynamodb.Table('Garbage_collector_table')

my_topic = "$aws/things/garbage_collector_thing/shadow/update/accepted"

response = table.get_item(TableName='Garbage_collector_table', Key={'topic':my_topic})

for res in response:
print "result ",res

我收到以下错误

Traceback (most recent call last):
File "get-data-dynamodb-boto3.py", line 19, in <module>
response = table.get_item(TableName='Garbage_collector_table', Key={'topic': my_topic}) File
"/usr/local/lib/python2.7/dist-packages/boto3/resources/factory.py",
line 518, in do_action
response = action(self, *args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/boto3/resources/action.py",
line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params) File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line
258, in _api_call
return self._make_api_call(operation_name, kwargs) File /usr/local/lib/python2.7/dist-packages/botocore/client.py", line 548,
in _make_api_call
raise ClientError(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

我的代码中是否遗漏了什么?

最佳答案

您正在混合具有不同方法的资源和客户端对象。 More info is here .

boto3 资源的正确语法是:

response = table.get_item(Key={'topic': my_topic})

boto3 客户端的正确语法是:

client = boto3.client('dynamodb')
response = client.get_item(TableName='Garbage_collector_table', Key={'topic':{'S':str(my_topic)}})

引用:Boto3 - DynamoDB

关于python - 无法使用 python 从 AWS dynamodb 获取项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37589254/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com