示例演练 1:为存储桶配置通知 (消息目标:SNS 主题和 SQS 队列)
演练摘要
在此演练中,您在存储桶上添加请求 Amazon S3 执行以下操作的通知配置:
-
将
s3:ObjectCreated:*
类型的事件发布到 Amazon SQS 队列。 -
将
s3:ReducedRedundancyLostObject
类型的事件发布到 Amazon SNS 主题。
有关通知配置的信息,请参阅 配置 Amazon S3 事件通知。
您可以使用控制台执行这些步骤,无需编写任何代码。此外,还提供了使用适用于 Java 和 .NET 的 AWS 开发工具包的代码示例,因此您可以通过编程方式添加通知配置。
您将在此演练中执行以下操作:
-
创建 Amazon SNS 主题。
您可使用 Amazon SNS 控制台创建 SNS 主题并订阅该主题,以便发布到此主题的所有事件都传送给您。您将指定电子邮件作为通信协议。在创建主题后,Amazon SNS 会发送电子邮件。您必须单击电子邮件中的链接以确认订阅主题。
您可将访问策略附加到此主题以授予 Amazon S3 发布消息的权限。
-
创建 Amazon SQS 队列。
使用 Amazon SQS 控制台,可创建 SQS 队列。您可以通过编程方式访问 Amazon S3 发送到此队列的所有消息。但对于此演练,您将在控制台中验证通知消息。
您可将访问策略附加到此主题以授予 Amazon S3 发布消息的权限。
-
将通知配置添加到存储桶。
步骤 1:创建 Amazon SNS 主题
按照以下步骤创建并订阅 Amazon Simple Notification Service (Amazon SNS) 主题。
-
使用 Amazon SNS 控制台创建主题。有关说明,请参阅 Amazon Simple Notification Service 开发人员指南 中的创建主题。
-
订阅至主题。对于此练习,请使用电子邮件作为通信协议。有关说明,请参阅 Amazon Simple Notification Service 开发人员指南 中的订阅主题。
您将收到电子邮件要求您确认订阅该主题。确认订阅。
-
使用以下策略替换附加到主题的访问策略。您必须通过提供您的 SNS 主题 ARN 和存储桶名称来更新策略:
{ "Version": "2008-10-17", "Id": "example-ID", "Statement": [ { "Sid": "example-statement-ID", "Effect": "Allow", "Principal": { "AWS":"*" }, "Action": [ "SNS:Publish" ], "Resource": "
SNS-topic-ARN
", "Condition": { "ArnLike": { "aws:SourceArn": "arn:aws-cn:s3:*:*:bucket-name
" } } } ] } -
记录主题 ARN。
您创建的 SNS 主题是您的 AWS 账户中的另一项资源,它有唯一的 Amazon 资源名称 (ARN)。在下一步骤中,您需要用到此 ARN。ARN 格式如下:
arn:aws-cn:sns:
aws-region
:account-id
:topic-name
步骤 2:创建 Amazon SQS 队列
按照以下步骤创建并订阅 Amazon Simple Queue Service (Amazon SQS) 队列。
-
使用 Amazon SQS 控制台创建队列。有关说明,请参阅 Amazon Simple Queue Service 开发人员指南 中的 Amazon SQS 入门。
-
使用以下策略替换附加到队列的访问策略 (在 SQS 控制台中,选择队列,然后在 Permissions 选项卡中,单击 Edit Policy Document (Advanced))。
{ "Version": "2012-10-17", "Id": "example-ID", "Statement": [ { "Sid": "example-statement-ID", "Effect": "Allow", "Principal": { "AWS":"*" }, "Action": [ "SQS:SendMessage" ], "Resource": "
SQS-queue-ARN
", "Condition": { "ArnLike": { "aws:SourceArn": "arn:aws-cn:s3:*:*:bucket-name
" } } } ] } -
(可选)如果 Amazon SQS 队列已启用服务器端加密 (SSE),则可将以下策略添加到关联的自定义 AWS Key Management Service (AWS KMS) 客户主密钥 (CMK)。您必须将策略添加到自定义 CMK,因为无法修改 Amazon SQS 的默认 AWS 托管 CMK。有关将 SSE 用于 Amazon SQS 以及 AWS KMS 的更多信息,请参阅使用服务器端加密 (SSE) 和 AWS KMS 保护数据。
{ "Version": "2012-10-17", "Id": "example-ID", "Statement": [ { "Sid": "example-statement-ID", "Effect": "Allow", "Principal": { "Service": "s3.amazonaws.com" }, "Action": [ "kms:GenerateDataKey", "kms:Decrypt" ], "Resource": "*" } ] }
-
记录队列 ARN
您创建的 SQS 队列是您的 AWS 账户中的另一项资源,它有唯一的 Amazon 资源名称 (ARN)。在下一步骤中,您需要用到此 ARN。ARN 格式如下:
arn:aws-cn:sqs:
aws-region
:account-id
:queue-name
步骤 3:将通知配置添加到存储桶
您可以使用 Amazon S3 控制台或以编程方式使用 AWS 软件开发工具包启用存储桶通知。选择任何一个选项以配置存储桶通知。此部分提供使用适用于 Java 和 .NET 的 AWS 开发工具包的代码示例。
步骤 3 (选项 a) :使用控制台在存储桶上启用通知
使用 Amazon S3 控制台,添加请求 Amazon S3 执行以下操作的通知配置:
-
将
s3:ObjectCreated:*
类型的事件发布到您的 Amazon SQS 队列。 -
将
s3:ReducedRedundancyLostObject
类型的事件发布到您的 Amazon SNS 主题。
在您保存通知配置后,Amazon S3 将发布测试消息,您将通过电子邮件收到该消息。
有关说明,请参阅 Amazon Simple Storage Service 控制台用户指南 中的如何为 S3 存储桶启用和配置事件通知?
步骤 3 (选项 b) :使用适用于 .NET 的 AWS 开发工具包在存储桶上启用通知
下面的 C# 代码示例提供一个完整的代码列表,用于为存储桶添加通知配置。您需要更新该代码,提供您的存储桶名称和 SNS 主题 ARN。有关如何创建和测试有效示例的信息,请参阅 运行 Amazon S3 .NET 代码示例。
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.) using Amazon.S3; using Amazon.S3.Model; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Amazon.DocSamples.S3 { class EnableNotificationsTest { private const string bucketName = "*** bucket name ***"; private const string snsTopic = "*** SNS topic ARN ***"; private const string sqsQueue = "*** SQS topic ARN ***"; // Specify your bucket region (an example region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static IAmazonS3 client; public static void Main() { client = new AmazonS3Client(bucketRegion); EnableNotificationAsync().Wait(); } static async Task EnableNotificationAsync() { try { PutBucketNotificationRequest request = new PutBucketNotificationRequest { BucketName = bucketName }; TopicConfiguration c = new TopicConfiguration { Events = new List<EventType> { EventType.ObjectCreatedCopy }, Topic = snsTopic }; request.TopicConfigurations = new List<TopicConfiguration>(); request.TopicConfigurations.Add(c); request.QueueConfigurations = new List<QueueConfiguration>(); request.QueueConfigurations.Add(new QueueConfiguration() { Events = new List<EventType> { EventType.ObjectCreatedPut }, Queue = sqsQueue }); PutBucketNotificationResponse response = await client.PutBucketNotificationAsync(request); } catch (AmazonS3Exception e) { Console.WriteLine("Error encountered on server. Message:'{0}' ", e.Message); } catch (Exception e) { Console.WriteLine("Unknown error encountered on server. Message:'{0}' ", e.Message); } } } }
步骤 3 (选项 c):使用AWS SDK for Java在存储桶上启用通知
以下示例说明如何将通知配置添加到存储桶。有关创建和测试有效示例的说明,请参阅测试 Amazon S3 Java 代码示例。
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.) import java.io.IOException; import java.util.EnumSet; import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.BucketNotificationConfiguration; import com.amazonaws.services.s3.model.TopicConfiguration; import com.amazonaws.services.s3.model.QueueConfiguration; import com.amazonaws.services.s3.model.S3Event; import com.amazonaws.services.s3.model.SetBucketNotificationConfigurationRequest; public class EnableNotificationOnABucket { public static void main(String[] args) throws IOException { String bucketName = "*** Bucket name ***"; String clientRegion = "*** Client region ***"; String snsTopicARN = "*** SNS Topic ARN ***"; String sqsQueueARN = "*** SQS Queue ARN ***"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(clientRegion) .build(); BucketNotificationConfiguration notificationConfiguration = new BucketNotificationConfiguration(); // Add an SNS topic notification. notificationConfiguration.addConfiguration("snsTopicConfig", new TopicConfiguration(snsTopicARN, EnumSet.of(S3Event.ObjectCreated))); // Add an SQS queue notification. notificationConfiguration.addConfiguration("sqsQueueConfig", new QueueConfiguration(sqsQueueARN, EnumSet.of(S3Event.ObjectCreated))); // Create the notification configuration request and set the bucket notification configuration. SetBucketNotificationConfigurationRequest request = new SetBucketNotificationConfigurationRequest( bucketName, notificationConfiguration); s3Client.setBucketNotificationConfiguration(request); } catch(AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch(SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }
步骤 4:测试设置
现在,您可以通过将对象上传到存储桶来测试设置,并在 Amazon SQS 控制台中验证事件通知。有关说明,请参阅 《Amazon Simple Queue Service 开发人员指南》的“开始使用”部分 中的接收消息。