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

道格

我试图第一次使用update_item将数据保存在dynamodb中。在项目的另一个区域中,我成功使用了put_item()。对于此新的代码区域,我仅保存更改的项目,而在db中保留未更改的项目。因此,我需要使用update_item()。但是,我似乎无法弄清楚为什么我的语法对于API调用不正确。我直接在Amazon UI中使用它。

这是我的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

这是输出:

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

我研究了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

我在语法上有一些变化,包括添加字典{“ S”:“也许可行”}而不是我的变量val,并且还尝试将变量更改为一些静态内容以查看它是否有效,但是没有运气。

显然,这是一个语法问题,但是我一直无法找到它。有什么建议吗?

鲍里斯·塞列布罗夫(Boris Serebrov)

我认为您使用的示例所基于的boto2界面与相比有很大不同boto3

相反,请查看boto3文档,您应该将关键字参数用作错误状态(并且您正在使用字典)。您的请求应大致如下所示:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章