Sie können Bucket-Mitteilungen entweder über die Amazon S3 Konsole oder programmatisch über die AWS SDKs aktivieren. Wählen Sie eine der Optionen, um Mitteilungen für Ihren Bucket zu konfigurieren. In diesem Abschnitt finden Sie Codebeispiele, in denen die AWS SDKs für Java und .NET verwendet werden.
Option A: Mitteilungen für einen Bucket über die Konsole aktivieren
Fügen Sie über die Amazon S3 Konsole eine Mitteilungskonfiguration hinzu, die Amazon S3 zu folgendem Verhalten auffordert:
Ereignisse des Typs All object create events (Alle Objekterstellungsereignisse ) in Ihrer Amazon SQS Warteschlange veröffentlichen.
Ereignisse des Typs Object in RRS lost (Objekt in RRS verloren) in Ihrem Amazon SNS Thema veröffentlichen.
Nachdem Sie die Mitteilungskonfiguration gesichert haben, sendet Amazon S3 eine Testnachricht, die Sie per E-Mail erhalten.
Eine Anleitung finden Sie unter Enabling and configuring event notifications using the Amazon S3 console (Aktivieren und Konfigurieren von Ereignismitteilungen über die Amazon S3 Konsole).
Option B: Mitteilungen für einen Bucket über die AWS SDKs aktivieren
Das folgende C# Codebeispiel enthält alle Code-Einträge, um eine Mitteilungskonfiguration zu einem Bucket hinzuzufügen.
Sie müssen den Code aktualisieren und Ihren Bucket-Namen und SNS Themen-ARN angeben. Siehe Schritt 2: Erstellen eines Amazon SNS-Themas für eine Anleitung zum Erstellen und Testen eines funktionierenden Beispiels.
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);
}
}
}
}
Das folgende .NET Codebeispiel zeigt, wie eine Mitteilungskonfiguration zu einem Bucket hinzugefügt werden kann.
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();
}
}
}
Sie können die Einrichtung jetzt testen, indem Sie ein Objekt in Ihren Bucket hochladen und die Ereignismitteilung in der Amazon SQS Konsole überprüfen.
Weitere Informationen finden Sie unter Receiving a Message (Empfangen einer Nachricht) im Amazon Simple Queue Service Developer Guide "Getting Started" section (Abschnitt „Erste Schritte“ im Amazon Simple Queue Service Leitfaden für Entwickler).
Gehen Sie nach Testen der Einrichtung zu Einrichten der Datenweiterleitung an Google Security Operations zurück und fügen Sie den Amazon S3 Bucket hinzu, um die SQS Weiterleitung an Google Security Operations einzurichten.