首页 > 解决方案 > SNS 主题的多个电子邮件端点

问题描述

我有一个由 S3 存储桶中的 PUT 事件触发的 Lambda 函数。此功能需要向 SNS 主题的所有订阅者发送电子邮件。

Lambda 函数代码为:

import json
import boto3

print('Loading function')

s3 = boto3.client('s3')
sns = boto3.client('sns')

def lambda_handler(event, context):
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = event['Records'][0]['s3']['object']['key']
    eventname = event['Records'][0]['eventName']
    sns_message = str("A new file has been uploaded in our S3 bucket. Please find the details of file uploaded belown\n\nBUCKET NAME: "+ bucket +"\nFILE NAME: " + key)
    try:
        if eventname == "ObjectCreated:Put":
            subject= "New data available in  S3 Bucket [" + bucket +"]"
            sns_response = sns.publish(TargetArn='<SNS ARN ID>',Message= str(sns_message),Subject= str(subject))
    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

但是,仅向订阅 SNS 主题的一个电子邮件端点触发电子邮件。知道我错过了什么吗?

标签: amazon-web-servicesaws-lambdaboto3amazon-sns

解决方案


我的代码中有错误。我没有使用 TopicArn,而是使用了 TargetArn。将代码更改为 TopicArn 解决了这个问题。


推荐阅读