代码之家  ›  专栏  ›  技术社区  ›  Wesley Schravendijk

使用python从dynamodb获取\u项

  •  2
  • Wesley Schravendijk  · 技术社区  · 7 年前

    这个问题此刻正悄悄地折磨着我。 我正在努力学习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)"]]}
    

    enter image description here

    我的DynamoDb设置 表名html\U内容 主分区密钥id(编号) 禁用时间点恢复 已禁用加密 生存时间属性禁用管理TTL 表格状态激活

    先谢谢你。

    卫斯理

    1 回复  |  直到 7 年前
        1
  •  3
  •   Wesley Schravendijk    7 年前

    这是给@ippi的。

    我用的是引语。

    table.get_item(Key={"id": '1'})
    

    必须是

    table.get_item(Key={"id": 1})
    

    因为我使用的是数字而不是字符串。