【发布时间】:2021-08-16 20:10:20
【问题描述】:
我有一个由 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-services aws-lambda boto3 amazon-sns