使用 python 从 dynamodb 获取_item

Wes*_*ijk 6 amazon-dynamodb aws-lambda

这个问题此刻正在轻轻地杀死我。我正在尝试学习 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'})
Run Code Online (Sandbox Code Playgroud)

我到处都看到我应该这样做。但我不断收到以下错误

{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)"]]}
Run Code Online (Sandbox Code Playgroud)

我的数据库结构。

在此输入图像描述

我的 DynamoDb 设置 表名称 html_contents 主分区键 ID(数字) 主排序键 - 时间点恢复 DISABLED启用加密 DISABLED 生存时间属性 DISABLED管理 TTL 表状态 活动

我在这里做错了什么?我开始认为我的 aws 配置出了问题。

先感谢您。

韦斯利

Wes*_*ijk 3

致@ippi。

这是我正在使用的引号。

table.get_item(Key={"id": '1'})
Run Code Online (Sandbox Code Playgroud)

需要是

table.get_item(Key={"id": 1})
Run Code Online (Sandbox Code Playgroud)

因为我使用的是数字而不是字符串。希望这对下一个遇到同样问题的人有所帮助。