简体   繁体   English

Amazon lambda dynamodb update_item() 只接受关键字参数

[英]Amazon lambda dynamodb update_item() only accepts keyword arguments

I am trying to save data in dynamodb using update_item for the first time.我第一次尝试使用 update_item 在 dynamodb 中保存数据。 In another area of my project I have used put_item() successfully.在我项目的另一个区域中,我成功地使用了 put_item()。 For this new area of code I am saving only items that change, leaving items in the db that are unchanged.对于这个新的代码区域,我只保存更改的项目,而保留数据库中未更改的项目。 Thus, I need to use update_item().因此,我需要使用 update_item()。 However, I can't seem to figure out why my syntax is not correct for the API call.但是,我似乎无法弄清楚为什么我的语法对于 API 调用不正确。 I am using this directly from the Amazon UI.我直接从亚马逊用户界面使用它。

Here is my python code:这是我的python代码:

from __future__ import print_function
import json
import boto3

print('Loading function')

def saveScreenData(event, context):

    dynamodb = boto3.client('dynamodb', region_name='us-east-1', endpoint_url="https://dynamodb.us-east-1.amazonaws.com")

    print('The event: {}'.format(event))

    key = {}
    key['UID'] = event['uid']
    key['screenId'] = event['screenid']
    print('Key: {}'.format(key))

    for item, val in event.items():

        if item != 'uid' and item != 'screenid':
            print("Saving!")
            response = dynamodb.update_item({ 
                "TableName" : "ScreenData",
                "Key" : key,
                "UpdateExpression" : "SET #attrName = :attrValue",
                "ExpressionAttributeNames" : {
                    "#attrName" : item
                },
                "ExpressionAttributeValues" : {
                    ":attrValue" : val
                }
            })

            print('Response: {}'.format(response))


    return response

Here is the output:这是输出:

START RequestId: 2da9412a-b03d-11e7-9dc8-8fcb305833f6 Version: $LATEST
The event: {'article': '<p>↵    First!↵</p>', 'screenid': '13', 'uid': '0', 'section1': '<h1>↵    Second↵</h1>'}
Key: {'UID': '0', 'screenId': '13'}
Saving!
update_item() only accepts keyword arguments.: TypeError
Traceback (most recent call last):
  File "/var/task/saveScreenData.py", line 30, in saveScreenData
    ":attrValue" : val
  File "/var/runtime/botocore/client.py", line 310, in _api_call
    "%s() only accepts keyword arguments." % py_operation_name)
TypeError: update_item() only accepts keyword arguments.

END RequestId: 2da9412a-b03d-11e7-9dc8-8fcb305833f6

I have researched the update_item docs ( https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html ) and have modeled my query after this SO q&a by mkobit ( https://stackoverflow.com/users/627727/mkobit ): https://stackoverflow.com/a/30604746/8027640我已经研究了 update_item 文档( https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html )并在 mkobit ( https://stackoverflow.com/users / 627727 / mkobit ): https://stackoverflow.com/a/30604746/8027640

I have played with variations on the syntax, including adding the dictionary {"S" : "maybe this works"} instead of my variable val, and have also tried changing the variable to some static content to see if it works, but no luck.我玩过语法的变化,包括添加字典 {"S" : "maybe this works"} 而不是我的变量 val,并且还尝试将变量更改为一些静态内容以查看它是否有效,但没有运气.

Clearly this is a syntax issue, but I have been unable to track it down.显然这是一个语法问题,但我一直无法追踪它。 Suggestions?建议?

I think the example your are using is based on boto2 which has quite different interface comparing to boto3 .我认为您使用的示例基于boto2 ,与boto3相比,它具有完全不同的界面。

Instead, look into the boto3 documentation , you should use keyword arguments as the error states (and you are using the dictionary).相反,查看boto3 文档,您应该使用关键字参数作为错误状态(并且您正在使用字典)。 Your request should look approximately like this:您的请求应大致如下所示:

response = dynamodb.update_item(
    TableName="ScreenData",
    Key=key,
    UpdateExpression="SET #attrName = :attrValue",
    ExpressionAttributeNames={
        "#attrName" : item
    },
    ExpressionAttributeValues={
        ":attrValue" : val
    }
)

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

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