如何使用 python/Boto3 按文件名过滤 s3 新对象创建事件?

How to filter s3 new object created events by file name using python/Boto3?

我有一个名为:

在 bucket_1 我为在该存储桶中创建的所有新对象设置了 s3 事件。我想做的是设置一个规则,以便如果删除的文件以“数据”为前缀。然后它会触发一个 lambda 函数来处理文件中的数据。

我正在苦苦挣扎的是如何过滤特定文件。到目前为止,我在 python 中的代码是这样的:

def handler(event, context):
    s3 = boto3.resource('s3')
    message = json.loads(event['Records'][0]['Sns']['Message'])
    print("JSON: " + json.dumps(message))


    return message

当事件添加到我的 sns 主题时会触发此 lambda,但我只想专门针对前缀为“数据”的对象创建进行过滤。

有没有人做过类似的事情?为清楚起见,这是我希望完成的工作的工作流程:

1. file added to bucket_1
2. notification sent to sns topic [TRIGGERS below python code]
3. python filters for object created notification and file with prefix of "data*" [Triggers] python below]
4. python fetched data from s3 location cleans it up and places it into table.

具体来说,我正在研究如何准确设置第 3 步。

环顾四周后,我发现这实际上并没有在 python 中完成,而是在 s3 存储桶本身上配置事件时完成的。

你去这个地区: s3 > 选择您的存储桶 > 属性 > 事件通知 > 创建通知

进入后,您只需 select 所有创建对象事件,您甚至可以指定文件名的前缀和后缀。然后,您将只会收到针对您指定的主题的通知。

希望这对某些人有所帮助!