您可以使用Amazon S3主控台,或使用AWS SDK以程式設計方式啟用貯體通知。選擇任一選項來配置貯體上的通知。本節提供使用適用於Java和 .NET的AWS SDK的程式碼範例。
選項A:使用主控台在貯體上啟用通知
使用Amazon S3主控台,新增請求Amazon S3執行以下操作的通知配置:
將所有物件建立事件類型的事件發佈到您的Amazon SQS佇列。
將RRS中的物件遺失類型的事件發佈到您的Amazon SNS主題。
儲存通知配置後,Amazon S3會發佈一則測試訊息,您可以透過電子郵件收到該訊息。
有關說明,請參閱Enabling and configuring event notifications using the Amazon S3 console(使用Amazon S3主控台啟用和配置事件通知)。
選項B:使用AWS SDK在貯體上啟用通知
以下C#程式碼範例提供了將通知配置新增至貯體的完整程式碼清單。
您必須更新程式碼並提供您的貯體名稱和SNS主題ARN。請參閱步驟2:建立Amazon SNS主題以取得如何建立和測試工作範例的說明。
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);
}
}
}
}
下列.NET程式碼範例顯示如何將通知配置新增至貯體。
有關如何建立和測試工作範例的說明,請參閱Testing the Amazon S3 Java Code Examples(測試Amazon S3 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();
}
}
}
您現在可以透過將物件上傳到貯體,並在Amazon SQS主控台中驗證事件通知來測試設定。
請參閱Amazon Simple Queue Service開發者指南「開始使用」一節的Receiving a Message(接收訊息)來瞭解更多資訊。
測試設定後,返回設定將資料轉寄到Google Security Operations,並新增Amazon S3貯體以設定將SQS轉寄到Google Security Operations。