1

我想在将文件上传到 s3 存储桶时收到电子邮件,如上面的标题所述,我使用的是无服务器,问题是我在 s3 上创建的事件只在 s3-aws 控制台上给我通知,并且不要不知道如何在 S3 上配置 cloudwatch 事件来触发 lambda。所以,如果有人知道如何使用 cloudwatch 在 S3 上触发事件,我会全力以赴。这是我的代码:

import json
import boto3
import botocore
import logging
import sys
import os
import traceback


from botocore.exceptions import ClientError
from pprint import pprint
from time import strftime, gmtime

email_from = '*****@******.com'
email_to = '******@******.com'
#email_cc = '********@gmail.com'
email_subject = 'new event on s3 '
email_body = 'a new file is uploaded'


#setup simple logging for INFO
logger = logging.getLogger()
logger.setLevel(logging.INFO)

from botocore.exceptions import ClientError

def sthree(event, context):
 """Send email whenever a file is uploaded to S3"""
body = {}
status_code = 200


try:
    s3 = boto3.client('s3')
    ses = boto3.client('ses')
    response = ses.send_email(Source = email_from,
                    Destination = {'ToAddresses': [email_to,],}, 
                        Message = {'Subject': {'Data': email_subject}, 'Body':{'Text' : {'Data': email_body}}}
         )

response = {
    "statusCode": 200,
    "body": json.dumps(body)
}

return response

这是我的 serverless.yml 文件

service: aws-python # NOTE: update this with your service name

plugins:
  - serverless-external-s3-event

provider:   name: aws   
runtime: python2.7 
stage: dev  
region: us-east-1   
iamRoleStatements:
        - Effect: "Allow"
          Action:
              - s3:*
              - "ses:SendEmail"
              - "ses:SendRawEmail"
              - "s3:PutBucketNotification"
          Resource: "*"

 functions:   sthree:
    handler: handler.sthree
    description: send mail whenever a file is uploaded on S3 
    events:
       - s3:
          bucket: cartegie-nirmine
          event: s3:ObjectCreated:*
          rules:
            - prefix: uploads/
            - suffix: .jpg
      - cloudwatchEvent:
              description: 'CloudWatch Event triggered '
              event:
                  source:
                      - "aws.S3"
                  detail-type:
                      - "S3 event Notification"
              enabled : true
4

1 回答 1

12

如果您的座右铭只是接收有关 S3 存储桶操作的电子邮件通知,那么您不需要 lambda 函数。对于问题中提到的用例,您可以使用 SNS 主题和 S3 事件来实现。我将提到从控制台遵循的步骤(通过 sdk 或 cli 可以实现相同的步骤)。

1) 创建一个Topic使用 SNS 控制台。

2)订阅主题。用作email通信协议并提供您的电子邮件 ID

3) 您将收到电子邮件,要求您确认订阅该主题。确认订阅。

4)重要:将主题的访问策略替换为以下策略:

{
  "Version": "2008-10-17",
  "Id": "__default_policy_ID",
  "Statement": [
    {
      "Sid": "__default_statement_ID",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "SNS:Publish",
      "Resource": "sns-topic-arn",
      "Condition": {
        "ArnLike": {
          "aws:SourceArn": "arn:aws:s3:*:*:s3-bucket-name"
        }
      }
    }
  ]
}

基本上,您允许您的s3-bucket发布到 SNS 主题。

sns-topic-arn替换为您在上面创建的主题的 ARN。将 s3 -bucket-name替换为您希望接收通知的存储桶名称。

5) 转到 S3 控制台。单击您的 S3 存储桶并打开“属性”选项卡。

6) 在高级设置下,点击事件卡。

7) 单击添加通知并输入值。下面显示了一个示例。

在此处输入图像描述

选择需要监控的 s3-events 和您创建的 SNS 主题。

8) 点击保存。现在您应该开始接收电子邮件通知。

于 2017-10-10T18:02:03.393 回答