如何使用 AWS SNS 服务打印电子邮件中新行中的每个字符串。
如果我在 Python 输出中打印一条消息,所有字符串都在新行中:
Started copying.. snapshot_id: snap-000000aeaada0000 from: region_src to: region_dst with new snapshot_id: new_snapshot_id
Started copying.. snapshot_id: snap-000000aeaada0001 from: region_src to: region_dst with new snapshot_id: new_snapshot_id
但在一封电子邮件中,所有内容都在一行中:
Started copying.. snapshot_id: snap-05b3834aeaada5a02 from: region_src to: region_dst with new snapshot_id: new_snapshot_id Started copying.. snapshot_id: snap-012d3db747de08d1f from: region_src to: region_dst with new snapshot_id: new_snapshot_id
import boto3
region_src = 'us-east-1'
sns_arn = "arn:aws:sns:us-east-1:000000000099:AWS_LAMBDA_NOTIFICATIONS"
def copy_snapshot_src_to_dst(snapshot_id):
message = ("Started copying.. snapshot_id: " + str(snapshot_id) + " from: " + "region_src" + " to: " + "region_dst" + " with new snapshot_id: " + "new_snapshot_id")
#print(message)
return message
def lambda_handler():
snapshots_id = [('snap-000000aeaada0000', [{'Key': 'Backup_Type', 'Value': 'Demo'}, {'Key': 'Backup Initiator Rule', 'Value': 'Daily-6d-retention'}, {'Key': 'Disaster_Recovery', 'Value': 'Full'}, {'Key': 'aws:backup:source-resource', 'Value': 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'}, {'Key': 'Name', 'Value': 'HOSTNAME'}]), ('snap-000000aeaada0001', [{'Key': 'Name', 'Value': 'HOSTNAME'}, {'Key': 'Backup Initiator Rule', 'Value': 'Daily-6d-retention'}, {'Key': 'Backup_Type', 'Value': 'Demo'}, {'Key': 'Disaster_Recovery', 'Value': 'Full'}, {'Key': 'aws:backup:source-resource', 'Value': 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF'}])]
message = ""
for i in snapshots_id:
snapshot_id = i[0]
message += copy_snapshot_src_to_dst(snapshot_id) + '\n'
print(message)
send_sns(message)
def send_sns(message):
if sns_arn:
print("Sending SNS alert")
sns = boto3.client("sns", region_name=region_src)
response = sns.publish(
TargetArn=sns_arn,
Subject=("AWS LAMBDA NOTIFICATION"),
Message=(message)
)
lambda_handler()
最佳答案
将 '\n'
替换为 ".\n"
,然后在电子邮件中 - 每个字符串都在一个新行中。
关于Python boto3 SNS 电子邮件格式(每个字符串换行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57726912/