You can enable bucket notifications either by using the Amazon S3 console or programmatically by using AWS SDKs. Choose any one of the options to configure notifications on your bucket. This section provides code examples using the AWS SDKs for Java and .NET.
Option A: Enable notifications on a bucket using the console
Using the Amazon S3 console, add a notification configuration requesting Amazon S3 to do the following:
Publish events of the All object create events type to your Amazon SQS queue.
Publish events of the Object in RRS lost type to your Amazon SNS topic.
After you save the notification configuration, Amazon S3 posts a test message, which you get via email.
For instructions, see Enabling and configuring event notifications using the Amazon S3 console.
Option B: Enable notifications on a bucket using the AWS SDKs
The following C# code example provides a complete code listing that adds a notification configuration to a bucket.
You must update the code and provide your bucket name and SNS topic ARN. See Step 2: Create an Amazon SNS topic for instructions on how to create and test a working sample.
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);
}
}
}
}
The following .NET code example shows how to add a notification configuration to a bucket.
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();
}
}
}
You can now test the setup by uploading an object to your bucket and verifying the event notification in the Amazon SQS console.
See Receiving a Message in the Amazon Simple Queue Service Developer Guide "Getting Started" section for more information.
After testing the setup, return to Setting Up Data Forwarding to Google Security Operations and add the Amazon S3 bucket to set up SQS forwarding to Google Security Operations.