简体   繁体   English

如何使用 python 中的 aws-cdk 设置 Amazon S3 通知以在您的存储桶中发生某些事件时接收通知?

[英]How do I set up Amazon S3 notification to receive notifications when certain events happen in your bucket using aws-cdk in python?

How do I set up Amazon S3 notification to receive notifications when certain events happen in your bucket using AWS-CDK in python?如何使用 Python 中的 AWS-CDK 设置 Amazon S3 通知以在您的存储桶中发生某些事件时接收通知? When I use the code below, I am able to set up the subscription.当我使用下面的代码时,我可以设置订阅。 But when a file gets uploaded to S3, it doesn't send me an email.但是当文件上传到 S3 时,它不会向我发送电子邮件。 When I manually update the access policy in the SNS console it works, but I don't know how to implement that in my CDK.当我在 SNS 控制台中手动更新访问策略时,它可以工作,但我不知道如何在我的 CDK 中实现它。 I have also attached the SNS access policy that I am trying to achieve but I am not getting that.我还附上了我试图实现的 SNS 访问策略,但我没有得到。 Here is my CDK snippet:这是我的 CDK 片段:

        #SNS

        sf_topic = aws_sns.Topic(
            self, "MyTopic",
            display_name="My Topic"
        )

        email=aws_sns_subscriptions.EmailSubscription("example@email.com")
        sf_topic.add_subscription(subscription=email)

        notification = aws_s3_notifications.SnsDestination(sf_topic)

        sf_bucket.add_event_notification(aws_s3.EventType.OBJECT_CREATED, notification)
{
 "Version": "2008-10-17",
 "Id": "example-ID",
 "Statement": [
  {
   "Sid": "s3-event-notifier",
   "Effect": "Allow",
   "Principal": {
     "Service": "s3.amazonaws.com"
   },
   "Action": [
    "SNS:Publish"
   ],
   "Resource": "<UPDATE-YOUR-SNS-ARN-HERE>",
   "Condition": {
      "ArnLike": {          
      "aws:SourceArn": "arn:aws:s3:*:*:<UPDATE-YOUR-BUCKET-NAME-HERE>"
    }
   }
  }
 ]
}

You should create an iam.PolicyStatement and attach it to your SNS topic.您应该创建一个 iam.PolicyStatement 并将其附加到您的 SNS 主题。

policyStatement = _iam.PolicyStatement(
    resources=[sf_topic.topic_arn],
    actions=[
        "sns:Publish"
    ],
    effect=_iam.Effect.ALLOW,
    conditions={"ArnLike": {"aws:SourceArn": sf_bucket.bucket_arn}}
)

sf_topic.add_to_resource_policy(policyStatement)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM