我们将一些从AWS RDS到SQL的SQL转储存储到GCP云存储中,作为长期的二级备份。在过去的四天中,数据没有传输到GCP,经分析发现,这是由于夏令时的变化并解决了该问题。
现在,我们将来希望从GCP收到一些通知,以通知电子邮件/短信-积极的情况/寻呼机的职责-消极的情况(此时仅电子邮件)。通过电子邮件发送邮件的方案是将新文件成功上传到GCS存储桶。但是由于Google对电子邮件或文本没有任何本机支持,请向我们提出解决方案。我已经配置了以下
已配置存储桶,以通知pub sub https://cloud.google.com/storage/docs/reporting-changes
将pub / sub通知配置为请求发送类型 https://cloud.google.com/storage/docs/pubsub-notifications
但是现在我该如何通过GCP发送电子邮件/文本。我们是主要的AWS商店。我们是否可以使用SES或SNS或任何其他类型的通知从PUB / SUB中获取数据。
答案 0 :(得分:0)
我们可以使用SES或SNS或任何其他类型的通知来获取 数据是否来自PUB / SUB?
是的。我已经使用带有发布/订阅触发器的Google Cloud Functions完成了此(SES)。我认为SNS一样容易。
但是,我发现设置Google Cloud Functions以使用SMTP客户端触发电子邮件要容易得多。我还使用Twilio触发了短信。
要使用AWS SNS或SES,您需要将boto3库与您的部署和AWS凭证打包在一起。您还可以使用AWS REST界面,因此不需要外部库。
对于电子邮件SMTP客户端,Google Cloud Functions包含smtplib
,因此这是一条非常简单的路径。您只需要一个电子邮件用户名和SMTP密码。为此,我使用Gmail或Office 365凭据。 20行python代码即可完成。
[编辑]
使用Google Cloud Functions发送电子邮件时,请使用SSL进行传输。对于Office 365和Gmail,此端口587。请勿尝试使用不使用SSL的端口25,因为几乎每个人都阻止端口25。
答案 1 :(得分:0)
教程:https://cloud.google.com/functions/docs/tutorials/storage
带有python 3.7的云功能可使用Amazon SES SMTP界面触发电子邮件。
import smtplib
import email.utils
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def email_notify(event, context):
"""Triggered by a change to a Cloud Storage bucket.
Args:
event (dict): Event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
file = event
print(f"Processing file: {file['name']}.")
BUCKET = file['bucket']
FILE = file['name']
CREATED = file['timeCreated']
UPDATED = file['updated']
# Replace sender@example.com with your "From" address.
# This address must be verified.
SENDER = 'sender-email-address'
SENDERNAME = 'no-reply'
# Replace recipient@example.com with a "To" address. If your account
# is still in the sandbox, this address must be verified.
RECIPIENT = 'recepient-email-address'
# Replace smtp_username with your Amazon SES SMTP user name.
USERNAME_SMTP = "SMTP-USERNAME"
# Replace smtp_password with your Amazon SES SMTP password.
PASSWORD_SMTP = "SMTP-PASSWORD"
# (Optional) the name of a configuration set to use for this message.
# If you comment out this line, you also need to remove or comment out
# the "X-SES-CONFIGURATION-SET:" header below.
# CONFIGURATION_SET = "ConfigSet"
# If you're using Amazon SES in an AWS Region other than US West (Oregon),
# replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP
# endpoint in the appropriate region.
HOST = "email-smtp.us-west-2.amazonaws.com"
PORT = 587
# The subject line of the email.
SUBJECT = 'Successfull upload of file {} to GCS bucket {}'.format(FILE,BUCKET)
# The email body for recipients with non-HTML email clients.
BODY_TEXT = ("File upload to GCS bucket\r\n"
"Bucket-Name: {}\r\n"
"File-Name: {}\r\n"
"File-Create-Date: {}\r\n"
"File-Update-Date: {}\r\n"
).format(BUCKET,FILE,CREATED,UPDATED)
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = SUBJECT
msg['From'] = email.utils.formataddr((SENDERNAME, SENDER))
msg['To'] = RECIPIENT
# Comment or delete the next line if you are not using a configuration set
# msg.add_header('X-SES-CONFIGURATION-SET',CONFIGURATION_SET)
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(BODY_TEXT, 'plain')
# part2 = MIMEText(BODY_HTML, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
# msg.attach(part2)
# Try to send the message.
try:
server = smtplib.SMTP(HOST, PORT)
server.ehlo()
server.starttls()
#stmplib docs recommend calling ehlo() before & after starttls()
server.ehlo()
server.login(USERNAME_SMTP, PASSWORD_SMTP)
server.sendmail(SENDER, RECIPIENT, msg.as_string())
server.close()
# Display an error message if something goes wrong.
except Exception as e:
print ("Error: ", e)
else:
print ("Email sent!")