我想檢查一個 dynamo db 事件是否為 INSERT。事件看起來像這樣:
{'Records': [{'eventID': '4ff7', 'eventName': 'INSERT', 'eventVersion': '1.1', 'eventSource': 'aws:dynamodb', 'awsRegion': 'eu-central-1', 'dynamodb': {'ApproximateCreationDateTime': 1637232247.0, 'Keys': {'filename': {'S': 'issues.zip'}}, 'NewImage': {'filetype': {'NULL': True}, 'filename': {'S': 'issues.zip'}, 'unixtimestamp': {'S': '1591282803734'}, 'masterclient': {'S': '100-ff0-uat'}, 'source_bucket_name': {'S': 'ems'}, 'filekey': {'S': 'incoming/100-ff0-uat/1591282803734/issues.zip'}}, 'SequenceNumber': '9000', 'SizeBytes': 232, 'StreamViewType': 'NEW_IMAGE'}, 'eventSourceARN': 'arn:aws:dynamodb:eu-central-1:table/filenames-ems/stream/2021-11-18T08:42:01.008'}]}
我正在嘗試這個:
if event['Records']['eventName'] == 'INSERT':
但它拋出TypeError: list indices must be integers or slices, not str。在字典中的串列中過濾字典的最佳方法是什么?
我也嘗試過搜索if value in event.values == 'INSERT'但也沒有作業
uj5u.com熱心網友回復:
由于Recordscontains list of dict,因此為了訪問eventName您必須首先遍歷串列:
嘗試:
for r in event["Records"]:
if r['eventName'] == 'INSERT':
# perform any process
pass
uj5u.com熱心網友回復:
Records是一個串列。您可以使用 來檢查特定記錄event['Records'][n],其中n是記錄的索引。或者您可以遍歷所有記錄以查看是否存在帶有“INSERT”的記錄。
events ={'Records': [{'eventID': '4ff7', 'eventName': 'INSERT', 'eventVersion': '1.1', 'eventSource': 'aws:dynamodb', 'awsRegion': 'eu-central-1', 'dynamodb': {'ApproximateCreationDateTime': 1637232247.0, 'Keys': {'filename': {'S': 'issues.zip'}}, 'NewImage': {'filetype': {'NULL': True}, 'filename': {'S': 'issues.zip'}, 'unixtimestamp': {'S': '1591282803734'}, 'masterclient': {'S': '100-ff0-uat'}, 'source_bucket_name': {'S': 'ems'}, 'filekey': {'S': 'incoming/100-ff0-uat/1591282803734/issues.zip'}}, 'SequenceNumber': '9000', 'SizeBytes': 232, 'StreamViewType': 'NEW_IMAGE'}, 'eventSourceARN': 'arn:aws:dynamodb:eu-central-1:table/filenames-ems/stream/2021-11-18T08:42:01.008'}]}
for record in events['Records']:
if record['eventName'] == 'INSERT':
#Do something with the record
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/360536.html
標籤:Python 蟒蛇-3.x 亚马逊网络服务 字典 亚马逊动态数据库
上一篇:如何使用exclusiveStartKey為dynamodbaws-sdk-go-v2進行分頁?
下一篇:Html影像不會改變
