AWS Lambda Boto3 python - dynamodb 表上的过滤器表达式抛出错误“errorMessage”:“名称'Attr'未定义”,

Jas*_*son -1 python boto3 aws-lambda

我在 dynamodb 表上使用过滤器。它会抛出以下错误。Boto3 文档显示 response = table.scan(FilterExpression=Attr('myattribute').eq('myvalue')

我也做了同样的事情。我想要此表中的项目,其中 agentRole = Receiver

  Response
  {
        "errorMessage": "name 'Attr' is not defined",
        "errorType": "NameError",
        "requestId": "1b2fbee6-5fa2-4951-8689-3d1bfec76e5c",
         "stackTrace": [
              "  File \"/var/task/lambda_function.py\", line 21, in lambda_handler\n    
          response = tableresource.scan(FilterExpression=Attr('agentRole').eq('Receiver'))\n"
       ]
   }
Run Code Online (Sandbox Code Playgroud)

这是代码:

   import json
   import os
   import boto3
   from pprint import pprint

    #Find records that has agentRole as 'Receiver'

    tableName = os.environ.get('TABLE')
    fieldName = os.environ.get('FIELD')
    keytofind = os.environ.get('FILTER')
    fieldname = "agentRole"
    dbclient = boto3.resource('dynamodb')

    def lambda_handler(event, context):
   
         tableresource = dbclient.Table(tableName)
         count = tableresource.item_count
         response = tableresource.scan(FilterExpression=Attr('agentRole').eq('Receiver'))
        
 
 
Run Code Online (Sandbox Code Playgroud)

jel*_*csc 5

from boto3.dynamodb.conditions import Attr
Run Code Online (Sandbox Code Playgroud)