【问题标题】:GCP Cloud Storage(GCS) new object / changes notifications to email / text ,messageGCP 云存储(GCS)新对象/更改通知到电子邮件/文本,消息
【发布时间】:2019-04-12 10:37:22
【问题描述】:

我们将一些 SQL 转储从 AWS RDS 存储到 GCP 云存储作为长期辅助备份。过去四天数据没有传输到 GCP,经分析我们发现这是因为夏令时更改并解决了这个问题。

现在,我们希望将来从 GCP 收到一些通知,发送到电子邮件/短信 - 积极情景/寻呼机职责 - 消极情景(此时仅电子邮件)。发送电子邮件的积极场景是将新文件成功上传到 GCS 存储桶。但由于谷歌对电子邮件或文本没有任何原生支持,请向我们建议一个解决方案。我已经配置了以下

  1. 配置存储桶以通知发布订阅的更改https://cloud.google.com/storage/docs/reporting-changes

  2. 将 pub /sub 通知配置为拉取传递类型 https://cloud.google.com/storage/docs/pubsub-notifications

但是现在我如何从 GCP 发送电子邮件/文本。我们是主要的 AWS 商店。我们可以使用 SES 或 SNS 或任何其他类型的通知从 PUB/SUB 中获取数据吗?

【问题讨论】:

    标签: google-cloud-platform google-cloud-storage amazon-sns amazon-ses google-cloud-pubsub


    【解决方案1】:

    我们可以使用 SES 或 SNS 或任何其他类型的通知来获取 PUB/SUB 中的数据?

    是的。我已经使用带有 Pub/Sub 触发器的 Google Cloud Functions 完成了这个(SES)。我假设 SNS 也一样简单。

    但是,我发现将 Google Cloud Functions 设置为使用 SMTP 客户端来触发电子邮件要容易得多。我还使用 Twilio 来触发 SMS 文本消息。

    要使用 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。

    【讨论】:

    • 我使用谷歌云功能通过 smtp 发送电子邮件,但云功能似乎阻止了 SMTP 端口。 [cloud.google.com/functions/docs/bestpractices/tips] 。你能告诉我你是怎么克服限制的吗?
    • 我没有任何问题,也不必进行任何更改。但是,我只使用 SMTP SSL。对于 Gmail 和 Office 365,这是端口 587。不要尝试使用几乎所有人都阻止的端口 25。您提供的链接已损坏(404 错误)。
    • 谢谢。实际链接:cloud.google.com/functions/docs/bestpractices/tips 我可以使用端口 587,并且电子邮件功能在 Amazon SES 上运行良好。只有 25 端口被阻塞。我在答案中给出了我的代码。另外我不确定它是否是一个新功能,Cloud 功能可以直接从 GCS 获取触发器,而无需中间的 pub/sub,我们可以将其配置为不同的功能,例如创建、删除等。所以每当有新文件创建时直接调用云端函数。
    【解决方案2】:
    1. 我们可以配置 GCS 存储桶直接触发云功能,以便对 GCS 存储桶进行以下更改。
      1. 完成 - 创建
      2. 删除
      3. 存档
      4. 元数据更新

    教程:https://cloud.google.com/functions/docs/tutorials/storage

    1. 带有 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!")
      

    【讨论】:

      猜你喜欢
      • 2019-06-02
      • 2017-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 1970-01-01
      相关资源
      最近更新 更多