Vous pouvez activer les notifications de compartiment soit en utilisant la console Amazon S3, soit par le biais de l’interface de programmation en utilisant les SDK AWS. Choisissez l’une de ces options pour configurer les notifications dans votre compartiment. Cette section propose des exemples de code utilisant les SDK AWS pour Java et .NET.
Option A : activer les notifications dans un compartiment à l’aide de la console
Utilisez la console Amazon S3 pour ajouter une configuration de notification qui demande à Amazon S3 :
de publier des événements de type All object create events (Tous les objets créent des événements) dans votre file d’attente Amazon SQS
de publier des événements de type Object in RRS lost (Objets dans RRS perdus) dans votre rubrique Amazon SNS
Après avoir enregistré la configuration de notification, Amazon S3 publie un message de test que vous recevez par courriel.
Pour des instructions, consultez la section Activation et configuration des notifications d’événements à l’aide de la console Amazon S3.
Option B : activer les notifications dans un compartiment à l’aide des SDK AWS
Le code C# suivant fourni à titre d’exemple offre un listing de code complet qui ajoute une configuration de notification dans un compartiment.
Vous devez obligatoirement mettre à jour ce code et indiquer le nom de votre compartiment ainsi que l’ARN de la rubrique SNS. Consultez l’article Étape 2 : créer une rubrique Amazon SNS pour obtenir des instructions sur la création et le test d’un échantillon valide.
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);
}
}
}
}
Le code .NET suivant fourni à titre d’exemple montre comment ajouter une configuration de notification dans un compartiment.
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();
}
}
}
Vous pouvez maintenant tester la configuration en téléversant un objet dans votre compartiment et en vérifiant la notification d’événement dans la console Amazon SQS.
Pour plus d’informations, consultez la section Réception d’un message du Guide du développeur Amazon Simple Queue Service (rubrique Premiers pas).
Après avoir testé la configuration, accédez à Configuration du transfert des données vers Google Security Operations et ajoutez le compartiment Amazon S3 pour configurer le transfert SQS vers Google Security Operations.