AWS DynamoDB 删除项目

问题描述 投票:0回答:1

我正在通过 Lambda 删除 DynamoDB 项目。

我有一个简单的表设置,分区键是日期(数字),并且没有排序键。 我在 AWS CLI 中(通过 CMD)测试了以下命令,它运行成功。

aws dynamodb delete-item --table-name serverlessApp --key '{\"date\":{\"N\":\"123\"}}'

但是,当我在 Lambda (Python 3.1.1) 中使用以下代码时,它显示错误。

table.delete_item(Key={"date":{"N":"123"}})

也测试了这个,但不起作用..

table.delete_item(Key={"date":{"N":"123"}, "message":{"S":"test"}})

你有什么想法吗?这是 DynamoDB JSON 视图和错误视图。

{
  "date": {
    "N": "123"
  },
  "message": {
    "S": "test"
  }
}
{
  "errorMessage": "An error occurred (ValidationException) when calling the DeleteItem operation: The provided key element does not match the schema",
  "errorType": "ClientError",
  "requestId": "ba83cf56-7b36-4dc9-a5be-29efed41c78c",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 21, in lambda_handler\n    table.delete_item(Key={\"date\":{\"N\":\"123\"}})\n",
    "  File \"/var/lang/lib/python3.11/site-packages/boto3/resources/factory.py\", line 580, in do_action\n    response = action(self, *args, **kwargs)\n",
    "  File \"/var/lang/lib/python3.11/site-packages/boto3/resources/action.py\", line 88, in __call__\n    response = getattr(parent.meta.client, operation_name)(*args, **params)\n",
    "  File \"/var/lang/lib/python3.11/site-packages/botocore/client.py\", line 534, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n",
    "  File \"/var/lang/lib/python3.11/site-packages/botocore/client.py\", line 976, in _make_api_call\n    raise error_class(parsed_response, operation_name)\n"
  ]
}
aws-lambda amazon-dynamodb boto
1个回答
0
投票

非常确定 boto3 会为您处理属性表达式。所以:

table.delete_item(Key={"date":{"N":"123"}})

将是:

table.delete_item(Key={"date": 123})
© www.soinside.com 2019 - 2024. All rights reserved.