我想在第一次使用update_item的dynamodb中保存數據。在我的項目的另一個領域,我成功地使用了put_item()。對於這個新的代碼區域,我只保存發生更改的項目,並在db中保留未更改的項目。因此,我需要使用update_item()。但是,我似乎無法弄清楚爲什麼我的語法不適合API調用。我直接從亞馬遜界面使用這個。亞馬遜lambda dynamodb update_item()只接受關鍵字參數
這裏是我的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)這SO q &一個通過後,已仿照我的查詢mkobit(https://stackoverflow.com/users/627727/mkobit):https://stackoverflow.com/a/30604746/8027640
我玩過各種語法,包括添加字典{「S」:「也許這工作」}而不是我的變量val,並且還嘗試將變量更改爲一些靜態內容以查看它是否有效,但沒有運氣。
顯然這是一個語法問題,但我一直無法追查到它。建議?
謝謝。還有以下需要更改:'key ['UID'] = {「S」:event ['uid']} key ['screenId'] = {「S」:event ['screenid']}'和'ExpressionAttributeNames = { 「#attrName」:STR(項目) }, ExpressionAttributeValues = { 「:attrValue」:{ 「S」:STR(VAL) } }' – Doug