バケット通知は、Amazon S3コンソールを使用するか、AWS SDK を使用してプログラムで有効にすることができます。バケットの通知を構成するには、いずれかのオプションを選択します。このセクションでは、Java および .NET 用の AWS SDK を使用したコード例を示します。
オプション A:コンソールを使用してバケットの通知を有効にする
Amazon S3コンソールを使用して、Amazon S3に次の操作を実行するようにリクエストする通知構成を追加します。
すべてのオブジェクト作成イベント (All object create events) タイプのイベントを Amazon SQS キューに公開します。
RRS 内のオブジェクト紛失 (Object in RRS lost) タイプのイベントを Amazon SNS トピックに公開します。
通知構成を保存すると、Amazon S3からポストされたテストメッセージが E メールで届きます。
手順については、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 コード例は、バケットに通知構成を追加する方法を示しています。
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 Developer Guide "Getting Started" section (Amazon Simple Queue Service 開発者ガイドの「はじめに」セクション) の Receiving a Message (メッセージの受信) を参照してください。
設定をテストした後、Google Security Operations へのデータ転送の設定 (Setting Up Data Forwarding to Google Security Operations) に戻り、Amazon S3バケットを追加して、Google Security Operations への SQS 転送を設定します。