【问题标题】:Getting data from DynamoDB从 DynamoDB 获取数据
【发布时间】:2019-06-25 07:29:41
【问题描述】:

我想使用 python 使用主键从 Dynamo-Db 表中获取数据。 我的表名是“CustomerDetails”,主键是“Email-id”。 我将从 API Gateway 触发这个 Lambda 函数。

      import boto3
      from boto3.dynamodb.conditions import Key, Attr
      dynamodb = boto3.resource('dynamodb')
      table = dynamodb.Table('CustomerDetails')
      def lambda_handler(event, context):
           response = table.get_item(key={'Email-id':event})
           #response = table.query(KeyConditionExpression=Key('Email-id').eq(event))

以上代码不工作。我在这里做错了什么?

【问题讨论】:

  • 删除了我的答案,因为正如 Mark B 所说,我正在查看 dynamodbClient 而不是 dynamodb.Table 话虽如此,您能否提供来自 table.get_item() 调用的响应?

标签: python-3.x amazon-web-services amazon-dynamodb boto3 api-gateway


【解决方案1】:

您必须将get_item(key= 更改为get_item(Key=,我认为您可能会告知从事件中检索email_id 的正确位置。

这是一个例子:

import boto3
from boto3.dynamodb.conditions import Key

def lambda_handler(event, context):
      dynamodb = boto3.resource('dynamodb')
      table = dynamodb.Table('CustomerDetails')
      email_id = event['xxx']['xxxx'] # where your email id is in the event dict

      response = table.get_item(Key={'Email-id': email_id}).get('Item')

      ...

【讨论】:

    猜你喜欢
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多