gpt4 book ai didi

python - AWS-Lambda:如何在 python 中处理 SNS 事件

转载 作者:行者123 更新时间:2023-12-04 16:00:06 25 4
gpt4 key购买 nike

如何从 SNS 事件中获取存储桶名称?

S3 存储桶将事件发布到 SNS 主题。本主题触发 Lambda。我需要 SNS,因为其他一些进程也在监听这个事件。所以我不能直接从 S3 触发 Lambda。

在 lambda 函数中,我得到两个参数:eventcontext我需要从事件中获取存储桶名称。我期待做这样的事情:

bucket = event['Records'][0]['s3']['bucket']['name']

但这不起作用,因为根据 Cloudwatch,事件参数如下所示:
{
"Records":[
{
"EventSource":"aws:sns",
"EventVersion":"1.0",
"EventSubscriptionArn":"arn:aws:sns:eu-central-1:123456:sns-topic-dam-poc-new-import1:823baaf4-93b6-499a-944e-c99e1a6b1fb5",
"Sns":{
"Type":"Notification",
"MessageId":"2330809c-29c4-5826-910d-d64a7ab22247",
"TopicArn":"arn:aws:sns:eu-central-1:123456:sns-topic-dam-poc-new-import1",
"Subject":"None",
"Message":"{\\'Records\\': [{\\'EventSource\\': \\'aws:sns\\', \\'EventVersion\\': \\'1.0\\', \\'EventSubscriptionArn\\': \\'arn:aws:sns:eu-central-1:123456:sns-topic-dam-poc-new-import1:823baaf4-93b6-499a-944e-c99e1a6b1fb5\\', \\'Sns\\': {\\'Type\\': \\'Notification\\', \\'MessageId\\': \\'b611d3b2-a0d4-55fe-af0f-85648ffc2a59\\', \\'TopicArn\\': \\'arn:aws:sns:eu-central-1:123456:sns-topic-dam-poc-new-import1\\', \\'Subject\\': \\'None\\', \\'Message\\': \\'{\\n \"Records\":[\\n {\\n \"EventSource\":\"aws:sns\",\\n \"EventVersion\":\"1.0\",\\n \"EventSubscriptionArn\":\"arn:aws:sns:eu-central-1:123456:sns-topic-dam-poc-new-import1:823baaf4-93b6-499a-944e-c99e1a6b1fb5\",\\n \"Sns\":{\\n \"Type\":\"Notification\",\\n \"MessageId\":\"906fd69c-ee31-50f2-8b9d-804910f6fdcd\",\\n \"TopicArn\":\"arn:aws:sns:eu-central-1:123456:sns-topic-dam-poc-new-import1\",\\n \"Subject\":\"Amazon S3 Notification\",\\n \"Message\":\"{\\\\\"Service\\\\\":\\\\\"Amazon S3\\\\\",\\\\\"Event\\\\\":\\\\\"s3:TestEvent\\\\\",\\\\\"Time\\\\\":\\\\\"2020-03-01T18:18:46.898Z\\\\\",\\\\\"Bucket\\\\\":\\\\\"s3-dam-poc-dev-inbox\\\\\",\\\\\"RequestId\\\\\":\\\\\"9534F72BD9216CA8\\\\\",\\\\\"HostId\\\\\":\\\\\"hSPdeBzns8wjrMHqkFJgw1UPMv0aZ/VOsAxw/z9wtboR+TOy8sKSKElDYxm9kszokgPsBU9T1nU=\\\\\"}\",\\n \"Timestamp\":\"2020-03-01T18:18:47.036Z\",\\n \"SignatureVersion\":\"1\",\\n \"Signature\":\"FuoBKJczUXuKJ58d5SdddqnOjMwjxzG2aN+sef/0M5MFsAjRMyCNknvaXVVRrhujK39HouoOoDqyJNFzDWYR3WTYAM0Sqws9wJK9zBV9mxmMYdIurfDmVj4xsF4xuSeaMy4y/VfU2r75DxqhE3MPmA02IuGInGJsmw9CTk+Ao+kdnk/6rJT+Bt3EKK7uKj3kIWYehPDCYKYdzCsDcaP+Aaj2qEhEbkZxdaopB+pHRkMsLhZS/vHcc0Vd1eyZkRYHkxrG4FeKXVZdJT5OGuyixOSWq9YdCBJhWRnq7cRlQnk05WfswWdukOw9yjBkidsT1pJoiBQ==\",\\n \"SigningCertUrl\":\"https://sns.eu-central-1.amazonaws.com/SimpleNotificationService-a86cb10b4e1f29c941702d7371d28f7b6.pem\",\\n \"UnsubscribeUrl\":\"https://sns.eu-central-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-central-1:123456:sns-topic-dam-poc-new-import1:823baaf4-93b6-499a-944e-c99e1a6b1fb5\",\\n \"MessageAttributes\":{\\n\\n }\\n }\\n }\\n ]\\n}\\', \\'Timestamp\\': \\'2020-03-01T21:45:12.192Z\\', \\'SignatureVersion\\': \\'1\\', \\'Signature\\': \\'pt7Mkfo+CB5rsHAnWWB7Kb/LtgBCZSJWc2fLTB84FhZ9/MFUK0pOYmou97dAgn1hdo48JEXnQKpnMN5flmxwGEmG6SPHklnSAgE18a0Bv0qQgzQ1KF9PIY2ulRJiebbuE0gToTlDCgiUGd0OJpZ5Cdr4TSZgOpT4AcDD4NcpGCBKMTRwTcllaQDTNWF2mko9y6iqLp08ptq14qm4ppu9hrdGUU3klxE77Psn+7El6Cv3R8/asdfasdfaksdjhfiqwezhfknabsdfiugasdkf33==\\', \\'SigningCertUrl\\': \\'https://sns.eu-central-1.amazonaws.com/SimpleNotificationService-ashk38dn298djhxo8he2lknfO.pem\\', \\'UnsubscribeUrl\\': \\'https://sns.eu-central-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-central-1:123456:sns-topic-dam-poc-new-import1:823baaf4-93b6-499a-944e-c99e1a6b1fb5\\', \\'MessageAttributes\\': {}}}]}",
"Timestamp":"2020-03-02T00:20:06.458Z",
"SignatureVersion":"1",
"SigningCertUrl":"https://sns.eu-central-1.amazonaws.com/SimpleNotificationService-d3iiiffndoendii3mmmdi3.pem",
"UnsubscribeUrl":"https://sns.eu-central-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-central-1:123456:sns-topic-dam-poc-new-import1:823baaf4-93b6-499a-944e-c99e1a6b1fb5",
"MessageAttributes":{

}
}
}
]
}

我可以在 Message 中看到存储桶名称
这是我的python代码:
import boto3
def newImportHandler(event, context):
print("__________")
print(event)
print("__________")
simple1 = event['Records'][0]['Sns']['Message']
print('Simple 1 '+simple1)
simple2 = simple1['Records'][0]
print('Simple 2 '+simple2)

simple1 是一个字符串!所以这段代码失败了!

最佳答案

为了重现您的情况,我执行了以下操作:

  • 创建了一个 Amazon S3 存储桶
  • 创建了一个 亚马逊 SNS 话题 并修改权限以接受来自存储桶的请求(取自 Example Walkthrough: Configure a Bucket for Notifications)
  • 创建了一个 AWS Lambda 函数 并订阅了SNS话题
  • 配置了一个 亚马逊 S3 事件 在bucket上发送消息到SNS主题
  • 通过将文件上传到存储桶进行测试

  • Lambda 代码对我来说很好用:
    import json

    def lambda_handler(event, context):

    message = json.loads(event['Records'][0]['Sns']['Message'])
    bucket = message['Records'][0]['s3']['bucket']['name']
    key = message['Records'][0]['s3']['object']['key']
    ...

    关于python - AWS-Lambda:如何在 python 中处理 SNS 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60481141/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com