这个问题此刻正在温柔地杀死我。
我正在尝试学习 python、lambda 和 Dynamodb。
Python 看起来很棒,我可以在使用像 Xampp 这样的普通 MySQL 服务器的同时连接到 MySQL,目标是学习使用 Dynamodb,但不知何故我无法从 Dynamodb 获取项目。这真的让我大吃一惊,而且已经过去两天了。
观看了大量 youtube 电影并阅读了 aws 文档。
任何线索我做错了什么。
我的代码到现在;
import json
import boto3
from boto3.dynamodb.conditions import Key, Attr
#always start with the lambda_handler
def lambda_handler(event, context):
# make the connection to dynamodb
dynamodb = boto3.resource('dynamodb')
# select the table
table = dynamodb.Table("html_contents")
# get item from database
items = table.get_item(Key={"id": '1'})
我到处都看到我应该这样做。
但我不断收到以下错误
{errorMessage=An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema, errorType=ClientError, stackTrace=[["\/var\/task\/lambda_function.py",16,"lambda_handler","\"id\": '1'"],["\/var\/runtime\/boto3\/resources\/factory.py",520,"do_action","response = action(self, *args, **kwargs)"],["\/var\/runtime\/boto3\/resources\/action.py",83,"__call__","response = getattr(parent.meta.client, operation_name)(**params)"],["\/var\/runtime\/botocore\/client.py",314,"_api_call","return self._make_api_call(operation_name, kwargs)"],["\/var\/runtime\/botocore\/client.py",612,"_make_api_call","raise error_class(parsed_response, operation_name)"]]}
我的数据库结构。
我的 DynamoDb 设置
表名 html_contents
主分区键 ID(数字)
主排序键 -
时间点恢复 DISABLEDEnable
加密已禁用
生存时间属性 DISABLEDManage TTL
表状态事件
我在这里做错了什么?我开始认为我的 aws 配置有问题。
先感谢您。
卫斯理
最佳答案
那是@ippi。
这是我正在使用的引号。
table.get_item(Key={"id": '1'})
需要
table.get_item(Key={"id": 1})
因为我使用的是数字而不是字符串。
希望这对下一个遇到同样问题的人有所帮助。
关于aws-lambda - 使用python从dynamodb获取_item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53311721/