演練:設定儲存貯體的通知 (SNS 主題或 SQS 佇列) - Amazon Simple Storage Service

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

演練:設定儲存貯體的通知 (SNS 主題或 SQS 佇列)

您可以使用 Amazon Simple Notification Service (Amazon SNS) 或 Amazon Simple Queue Service (Amazon SQS) 接收 Amazon S3 通知。在此演練中,您使用 Amazon SNS 主題和 Amazon SQS 佇列將通知設定新增至儲存貯體。

注意

Amazon Simple Queue Service FIFO (先進先出) 佇列不支援做為 Amazon S3 事件通知目的地。若要傳送 Amazon S3 事件通知給 Amazon SQS FIFO 佇列,您可以使用 Amazon EventBridge。如需詳細資訊,請參閱啟用 Amazon EventBridge

演練摘要

此演練可以協助您執行以下操作:

  • s3:ObjectCreated:* 類型的事件發佈至 Amazon SQS 佇列。

  • s3:ReducedRedundancyLostObject 類型的事件發佈至 Amazon SNS 主題。

如需通知組態的資訊,請參閱「使用 Amazon SQS、Amazon SNS 和 Lambda」。

您可以使用主控台執行所有這些步驟,不用撰寫任何程式碼。此外,還提供使用適用於 Java 和 .NET AWS SDKs 的程式碼範例,以協助您以程式設計方式新增通知組態。

此程序包含以下步驟:

  1. 建立 Amazon SQS 佇列。

    使用 Amazon SQS 主控台建立 SQS 佇列。您可以利用程式設計的方式,存取 Amazon S3 傳送到佇列的任何訊息。但在此演練中,您會在主控台中驗證通知訊息。

    您要將存取政策連接到佇列,以授予 Amazon S3 張貼訊息的許可。

  2. 建立 Amazon SNS 主題。

    使用 Amazon SNS 主控台,建立 SNS 主題並訂閱主題。如此一來,張貼其上的任何事件都會傳遞給您。您會將電子郵件指定為通訊協定。建立主題後,Amazon SNS 會傳送電子郵件。您可以使用電子郵件中的連結,確認主題訂閱。

    您要將存取政策連接到主題,以授予 Amazon S3 張貼訊息的許可。

  3. 將通知組態新增至儲存貯體。

步驟 1:建立 Amazon SQS 佇列

按照步驟建立和訂閱 Amazon Simple Queue Service (Amazon SQS) 佇列。

  1. 使用 Amazon SQS 主控台建立佇列。如需指示,請參閱《Amazon Simple Queue Service 開發人員指南》中的 Amazon SQS 入門

  2. 將連接至佇列的存取政策以下列政策取代。

    1. 在 Amazon SQS 主控台的 Queues (佇列) 清單中,選擇佇列名稱。

    2. Access policy (存取政策) 標籤中選擇 Edit (編輯)。

    3. 取代連接至佇列的存取政策。在其中,提供 Amazon SQS ARN、來源儲存貯體名稱以及儲存貯體擁有者帳戶 ID。

      { "Version": "2012-10-17", "Id": "example-ID", "Statement": [ { "Sid": "example-statement-ID", "Effect": "Allow", "Principal": { "Service": "s3.amazonaws.com" }, "Action": [ "SQS:SendMessage" ], "Resource": "SQS-queue-ARN", "Condition": { "ArnLike": { "aws:SourceArn": "arn:aws:s3:*:*:awsexamplebucket1" }, "StringEquals": { "aws:SourceAccount": "bucket-owner-account-id" } } } ] }
    4. 選擇 Save (儲存)。

  3. (選用) 如果 Amazon SQS 佇列或 Amazon SNS 主題已啟用伺服器端加密搭配 AWS Key Management Service (AWS KMS),請將下列政策新增至相關聯的對稱加密客戶受管金鑰。

    您必須將政策新增至客戶受管金鑰,因為您無法修改適用於 Amazon SQS 或 Amazon SNS 的 AWS 受管金鑰。

    { "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": "*" } ] }

    如需搭配 Amazon SQS 和 Amazon SNS 使用 SSE 的詳細資訊 AWS KMS,請參閱下列內容:

    • Amazon Simple Notification Service 開發人員指南》中的金鑰管理

    • Amazon Simple Queue Service 開發人員指南》中的金鑰管理

  4. 記下佇列 ARN。

    您建立的 SQS 佇列是 AWS 帳戶中的另一個資源。它具有獨特的 Amazon Resource Name (ARN)。下一個步驟需要用到此 ARN。ARN 的格式如下:

    arn:aws:sqs:aws-region:account-id:queue-name

步驟 2:建立 Amazon SNS 主題

按照步驟建立並訂閱 Amazon SNS 主題。

  1. 使用 Amazon SNS 主控台建立一個主題。如需說明,請參閱《Amazon Simple Notification Service 開發人員指南》中的建立 Amazon SNS 主題

  2. 訂閱此主題。在此練習中,請使用電子郵件作為通訊協定。如需詳細資訊,請參閱《Amazon Simple Notification Service 開發人員指南》中的訂閱 Amazon SNS 主題

    您會收到要求確認訂閱主題的電子郵件。確認訂閱。

  3. 下列列政策取代連接至主題的存取政策。在其中,提供您的 SNS 主題 ARN、儲存貯體名稱以及儲存貯體擁有者帳戶 ID。

    { "Version": "2012-10-17", "Id": "example-ID", "Statement": [ { "Sid": "Example SNS topic policy", "Effect": "Allow", "Principal": { "Service": "s3.amazonaws.com" }, "Action": [ "SNS:Publish" ], "Resource": "SNS-topic-ARN", "Condition": { "ArnLike": { "aws:SourceArn": "arn:aws:s3:*:*:amzn-s3-demo-bucket" }, "StringEquals": { "aws:SourceAccount": "bucket-owner-account-id" } } } ] }
  4. 請記下主題 ARN。

    您建立的 SNS 主題是 中的另一個資源 AWS 帳戶,且具有唯一的 ARN。下一個步驟需要用到此 ARN。ARN 的格式如下:

    arn:aws:sns:aws-region:account-id:topic-name

步驟 3:將通知組態新增至儲存貯體

您可以使用 Amazon S3 主控台或以程式設計方式使用 AWS SDKs來啟用儲存貯體通知。選擇任一選項在儲存貯體上設定通知。本節提供使用適用於 Java 和 .NET 的 AWS SDK 程式碼範例。

(選項 A):使用主控台啟用儲存貯體上的通知

使用 Amazon S3 主控台,新增請求 Amazon S3 的通知組態新增以執行下列操作:

  • 所有物件建立事件類型的事件發佈至您的 Amazon SQS 佇列。

  • RRS 中物件遺失 類型的事件發佈至您的 Amazon SNS 主題。

儲存通知組態後,Amazon S3 會張貼測試訊息,您會透過電子郵件收到此訊息。

如需說明,請參閱 使用 Amazon S3 主控台啟用和設定事件通知

選項 B:使用 AWS SDKs 啟用儲存貯體的通知

.NET

下列 C# 程式碼範例提供完整的程式碼清單,將通知組態新增至儲存貯體。您必須更新程式碼,並提供您的儲存貯體名稱和 SNS 主題 ARN。如需有關設定和執行程式碼範例的資訊,請參閱《適用於 .NET 的 AWS SDK 開發人員指南》中的適用於 .NET 的 SDK 入門AWS

using Amazon; 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); } } } }
Java

以下範例說明如何將通知設定新增至儲存貯體。如需如何建立和測試工作範例的說明,請參閱《 AWS SDK for Java 開發人員指南》中的入門

import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.*; import java.io.IOException; import java.util.EnumSet; public class EnableNotificationOnABucket { public static void main(String[] args) throws IOException { String bucketName = "*** Bucket name ***"; Regions clientRegion = Regions.DEFAULT_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 開發人員指南「入門」一節中的接收訊息