【发布时间】:2021-05-30 23:02:25
【问题描述】:
这个问题此刻正在悄悄地杀死我。 我正在尝试学习 python、lambda 和 Dynamodb。
Python 看起来很棒,我可以在使用像 Xampp 这样的普通 MySQL 服务器时连接到 MySQL,目标是学习使用 Dynamodb,但不知何故我无法从 Dynamodb 获取_items。这真的让我大吃一惊,而且已经过去两天了。
观看了大量的 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 配置有问题。
提前谢谢你。
韦斯利
【问题讨论】:
-
这里
table.get_item(Key={"id": '1'})您确定您的密钥是S(字符串)而不是N(数字)吗?'1'而不是1 -
键是一个数字,不是字符串,所以我应该只使用 1?就这么简单?
-
运气不好?我的下一个猜测是如果你的表有一个排序键,那么你也需要指定它。 (但这意味着您已经隐藏了该列,因为它不在您的屏幕截图中,请检查编辑列按钮(右侧表格上方的一个齿轮图标)
-
你是对的;这是不需要的报价。这么简单的事情,不敢相信我花了两天时间才弄明白。谢谢!高度赞赏。
标签: aws-lambda amazon-dynamodb