Boto扫描过滤器表达式:参数的类型无效

Rei*_*eid 0 amazon-dynamodb boto3 aws-lambda

我正在尝试使用以下代码,但无法使其正常工作。即使我完全按照文档中的说明进行操作,也会不断给我关于过滤器表达式类型错误的错误消息。我该怎么做才能解决此问题?

def EndpointDeleted(event):
    endpoint = event['Attributes']['EndpointArn']
    if('EndpointArn' in event['Attributes']):
        client = boto3.client('dynamodb')
        response = client.scan(
            TableName='sniffergps-mobilehub-812282467-Users',
            Select='ALL_ATTRIBUTES',
            FilterExpression=Attr('Endpoints').contains(endpoint)
        )
        return response
Run Code Online (Sandbox Code Playgroud)

但是我收到一个错误消息,说过滤器表达式是错误的类型。我有以下导入消息: import boto3 from boto3.dynamodb.conditions import Key from boto3.dynamodb.conditions import Attr

错误信息:

{
  "errorMessage": "Parameter validation failed:\nInvalid type for parameter FilterExpression, value: <boto3.dynamodb.conditions.Contains object at 0x7fdca25e0b38>, type: <class 'boto3.dynamodb.conditions.Contains'>, valid types: <class 'str'>",
  "errorType": "ParamValidationError",
  "stackTrace": [
    [
      "/var/task/lambda_function.py",
      13,
      "lambda_handler",
      "return EndpointDeleted(event)"
    ],
    [
      "/var/task/lambda_function.py",
      24,
      "EndpointDeleted",
      "FilterExpression=Attr('Endpoints').contains(endpoint)"
    ],
    [
      "/var/runtime/botocore/client.py",
      312,
      "_api_call",
      "return self._make_api_call(operation_name, kwargs)"
    ],
    [
      "/var/runtime/botocore/client.py",
      575,
      "_make_api_call",
      "api_params, operation_model, context=request_context)"
    ],
    [
      "/var/runtime/botocore/client.py",
      630,
      "_convert_to_request_dict",
      "api_params, operation_model)"
    ],
    [
      "/var/runtime/botocore/validate.py",
      291,
      "serialize_to_request",
      "raise ParamValidationError(report=report.generate_report())"
    ]
  ]
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*k B 6

注意Boto3 DynamoDB 客户端和表资源在语法上的区别。

FilterExpressionDynamoDB客户端的参数需要一个字符串。

用来设置FilterExpression参数的方法类似于使用DynamoDB.Table资源的方法。

  • 这个答案是不完整的。*如何*将语法从表一更改为客户端接受的语法? (6认同)