Boto Scan Filterexpression:参数的类型无效

时间:2017-10-17 17:43:35

标签: amazon-dynamodb aws-lambda boto3

我正在尝试使用以下代码,但我无法让它工作。它不断给我一个关于过滤器表达式是错误类型的错误消息,即使我正在完成the documentation中完成的操作。我该怎么做才能解决这个问题?

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

但是我收到错误,说过滤器表达式是不正确的类型。我有以下导入消息: 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())"
    ]
  ]
}

1 个答案:

答案 0 :(得分:6)

请注意Boto3 DynamoDB Client和表Resource之间的语法差异。

DynamoDB客户端的# Solve the sudoku def solve_sudoku(): # If there are no more empty cells, we are finished if(not empty_position_exists()): return True # Try numbers from 1 for posssible_number in range(1,10): if(position_valid(posssible_number,next_empty_pos[0],next_empty_pos[1])): sudoku[next_empty_pos[0]][next_empty_pos[1]] = posssible_number # If the next function call evalutes to true, then this should be true as well if(solve_sudoku()): return True # If the above did not work then, set the number back to 0 (unassgined) sudoku[next_empty_pos[0]][next_empty_pos[1]] = 0 # Return false if none of the numbers were good return False 参数需要一个字符串。

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