简体   繁体   English

AWS DynamoDB Update_Item 上的语法错误(lambda 函数)

[英]Syntax Error on AWS DynamoDB Update_Item (lambda function)

I am trying to update a current item in a dynamoDB table from a lambda function and am getting a syntax error with no information.我正在尝试从 lambda function 更新 dynamoDB 表中的当前项目,并且收到没有信息的语法错误。 I am baffled as I followed numerous online forums and blog posts to get to this stage, and the code seems identical.当我跟随许多在线论坛和博客文章到达这个阶段时,我感到很困惑,而且代码似乎相同。 My put_item snippet works as intended so dynamoDB has been connected to the lambda function correctly.我的 put_item 片段按预期工作,因此 dynamoDB 已正确连接到 lambda function。 Any help would be really appreciated.任何帮助将非常感激。

My code is as follows:我的代码如下:

update = client.update_item(
        TableName='sample',
        Key={'id': {'S': body["id"]},
        UpdateExpression="set cancelled = :g",
        ConditionExpression = 'attribute_exists(id)',
        ExpressionAttributeValues={
            ':g': "yes"
        },
        ReturnValues="UPDATED_NEW"
)

The logs I get are as follows:我得到的日志如下:

{
    "errorMessage": "Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 89)",
    "errorType": "Runtime.UserCodeSyntaxError",
    "requestId": "86785a61-c526-4647-9a2c-b51466ddb8a6",
    "stackTrace": [
    "  File \"/var/task/lambda_function.py\" Line 89\n         UpdateExpression=\"set canceled = :g\",\n"
  ]
}

After resolving this, I get the following error解决此问题后,我收到以下错误

{
  "errorMessage": "Parameter validation failed:\nInvalid type for parameter ExpressionAttributeValues.:g, value: yes, type: <class 'str'>, valid types: <class 'dict'>",
  "errorType": "ParamValidationError",
  "requestId": "b7710541-b398-4f2f-9f67-7eaafb1426bc",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 84, in lambda_handler\n    update = client.update_item(\n",
    "  File \"/var/runtime/botocore/client.py\", line 391, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n",
    "  File \"/var/runtime/botocore/client.py\", line 691, in _make_api_call\n    request_dict = self._convert_to_request_dict(\n",
    "  File \"/var/runtime/botocore/client.py\", line 739, in _convert_to_request_dict\n    request_dict = self._serializer.serialize_to_request(\n",
    "  File \"/var/runtime/botocore/validate.py\", line 360, in serialize_to_request\n    raise ParamValidationError(report=report.generate_report())\n"
  ]
}

Looks like you're choosing to interact with DynamoDB at the lowest wire level so you need to pass the "S" string type hint:看起来您选择在最低线路级别与 DynamoDB 交互,因此您需要传递“S”字符串类型提示:

":g": { "S": "yes" },

It can be confusing because you can interact with DynamoDB at various abstraction levels.这可能会让人感到困惑,因为您可以在各种抽象级别与 DynamoDB 进行交互。 I suggest going up a level and using .resource instead of .client .我建议提高一个级别并使用.resource而不是.client

Here's a sample where you don't need the "S" hints and things like that: https://github.com/aws-samples/aws-dynamodb-examples/blob/master/DynamoDB-SDK-Examples/python/WorkingWithItems/updating_item.py这是一个不需要“S”提示和类似内容的示例: https://github.com/aws-samples/aws-dynamodb-examples/blob/master/DynamoDB-SDK-Examples/python/WorkingWithItems /updating_item.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM