过滤字典(dynamodb 插入事件)

filter dictionary (dynamodb insert event)

我想检查 dynamo 数据库事件是否为 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',但也没有成功

由于 Records 包含 list of dict,因此要访问 eventName,您必须先循环遍历列表:

尝试:

for r in event["Records"]:
    if r['eventName'] == 'INSERT':
        # perform any process
        pass

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
if event['Records'][0]['eventName'] == 'INSERT':

对我有用