Use PutBucketNotificationConfiguration
with an Amazon SDK or command line tool
The following code examples show how to use PutBucketNotificationConfiguration
.
- .NET
-
- Amazon SDK for .NET
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example shows how to enable notifications for an Amazon Simple /// Storage Service (Amazon S3) bucket. /// </summary> public class EnableNotifications { public static async Task Main() { const string bucketName = "doc-example-bucket1"; const string snsTopic = "arn:aws:sns:us-east-2:0123456789ab:bucket-notify"; const string sqsQueue = "arn:aws:sqs:us-east-2:0123456789ab:Example_Queue"; IAmazonS3 client = new AmazonS3Client(Amazon.RegionEndpoint.USEast2); await EnableNotificationAsync(client, bucketName, snsTopic, sqsQueue); } /// <summary> /// This method makes the call to the PutBucketNotificationAsync method. /// </summary> /// <param name="client">An initialized Amazon S3 client used to call /// the PutBucketNotificationAsync method.</param> /// <param name="bucketName">The name of the bucket for which /// notifications will be turned on.</param> /// <param name="snsTopic">The ARN for the Amazon Simple Notification /// Service (Amazon SNS) topic associated with the S3 bucket.</param> /// <param name="sqsQueue">The ARN of the Amazon Simple Queue Service /// (Amazon SQS) queue to which notifications will be pushed.</param> public static async Task EnableNotificationAsync( IAmazonS3 client, string bucketName, string snsTopic, string sqsQueue) { try { // The bucket for which we are setting up notifications. var request = new PutBucketNotificationRequest() { BucketName = bucketName, }; // Defines the topic to use when sending a notification. var topicConfig = new TopicConfiguration() { Events = new List<EventType> { EventType.ObjectCreatedCopy }, Topic = snsTopic, }; request.TopicConfigurations = new List<TopicConfiguration> { topicConfig, }; request.QueueConfigurations = new List<QueueConfiguration> { new QueueConfiguration() { Events = new List<EventType> { EventType.ObjectCreatedPut }, Queue = sqsQueue, }, }; // Now apply the notification settings to the bucket. PutBucketNotificationResponse response = await client.PutBucketNotificationAsync(request); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } }
-
For API details, see PutBucketNotificationConfiguration in Amazon SDK for .NET API Reference.
-
- CLI
-
- Amazon CLI
-
The applies a notification configuration to a bucket named
my-bucket
:aws s3api put-bucket-notification --bucket my-bucket --notification-configuration file://notification.json
The file
notification.json
is a JSON document in the current folder that specifies an SNS topic and an event type to monitor:{ "TopicConfiguration": { "Event": "s3:ObjectCreated:*", "Topic": "arn:aws:sns:us-west-2:123456789012:s3-notification-topic" } }
The SNS topic must have an IAM policy attached to it that allows Amazon S3 to publish to it:
{ "Version": "2008-10-17", "Id": "example-ID", "Statement": [ { "Sid": "example-statement-ID", "Effect": "Allow", "Principal": { "Service": "s3.amazonaws.com" }, "Action": [ "SNS:Publish" ], "Resource": "arn:aws:sns:us-west-2:123456789012:my-bucket", "Condition": { "ArnLike": { "aws:SourceArn": "arn:aws:s3:*:*:my-bucket" } } } ] }
-
For API details, see PutBucketNotificationConfiguration
in Amazon CLI Command Reference.
-
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.Event; import software.amazon.awssdk.services.s3.model.NotificationConfiguration; import software.amazon.awssdk.services.s3.model.PutBucketNotificationConfigurationRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.TopicConfiguration; import java.util.ArrayList; import java.util.List; public class SetBucketEventBridgeNotification { public static void main(String[] args) { final String usage = """ Usage: <bucketName>\s Where: bucketName - The Amazon S3 bucket.\s topicArn - The Simple Notification Service topic ARN.\s id - An id value used for the topic configuration. This value is displayed in the AWS Management Console.\s """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String bucketName = args[0]; String topicArn = args[1]; String id = args[2]; Region region = Region.US_EAST_1; S3Client s3Client = S3Client.builder() .region(region) .build(); setBucketNotification(s3Client, bucketName, topicArn, id); s3Client.close(); } public static void setBucketNotification(S3Client s3Client, String bucketName, String topicArn, String id) { try { List<Event> events = new ArrayList<>(); events.add(Event.S3_OBJECT_CREATED_PUT); TopicConfiguration config = TopicConfiguration.builder() .topicArn(topicArn) .events(events) .id(id) .build(); List<TopicConfiguration> topics = new ArrayList<>(); topics.add(config); NotificationConfiguration configuration = NotificationConfiguration.builder() .topicConfigurations(topics) .build(); PutBucketNotificationConfigurationRequest configurationRequest = PutBucketNotificationConfigurationRequest .builder() .bucket(bucketName) .notificationConfiguration(configuration) .skipDestinationValidation(true) .build(); // Set the bucket notification configuration. s3Client.putBucketNotificationConfiguration(configurationRequest); System.out.println("Added bucket " + bucketName + " with EventBridge events enabled."); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
-
For API details, see PutBucketNotificationConfiguration in Amazon SDK for Java 2.x API Reference.
-
- PowerShell
-
- Tools for PowerShell
-
Example 1: This example configures the SNS topic configuration for the S3 event ObjectRemovedDelete and enables notification for the given s3 bucket
$topic = [Amazon.S3.Model.TopicConfiguration] @{ Id = "delete-event" Topic = "arn:aws:sns:eu-west-1:123456789012:topic-1" Event = [Amazon.S3.EventType]::ObjectRemovedDelete } Write-S3BucketNotification -BucketName kt-tools -TopicConfiguration $topic
Example 2: This example enables notifications of ObjectCreatedAll for the given bucket sending it to Lambda function.
$lambdaConfig = [Amazon.S3.Model.LambdaFunctionConfiguration] @{ Events = "s3:ObjectCreated:*" FunctionArn = "arn:aws:lambda:eu-west-1:123456789012:function:rdplock" Id = "ObjectCreated-Lambda" Filter = @{ S3KeyFilter = @{ FilterRules = @( @{Name="Prefix";Value="dada"} @{Name="Suffix";Value=".pem"} ) } } } Write-S3BucketNotification -BucketName ssm-editor -LambdaFunctionConfiguration $lambdaConfig
Example 3: This example creates 2 different Lambda configuration on the basis of different key-suffix and configured both in a single command.
#Lambda Config 1 $firstLambdaConfig = [Amazon.S3.Model.LambdaFunctionConfiguration] @{ Events = "s3:ObjectCreated:*" FunctionArn = "arn:aws:lambda:eu-west-1:123456789012:function:verifynet" Id = "ObjectCreated-dada-ps1" Filter = @{ S3KeyFilter = @{ FilterRules = @( @{Name="Prefix";Value="dada"} @{Name="Suffix";Value=".ps1"} ) } } } #Lambda Config 2 $secondlambdaConfig = [Amazon.S3.Model.LambdaFunctionConfiguration] @{ Events = [Amazon.S3.EventType]::ObjectCreatedAll FunctionArn = "arn:aws:lambda:eu-west-1:123456789012:function:verifyssm" Id = "ObjectCreated-dada-json" Filter = @{ S3KeyFilter = @{ FilterRules = @( @{Name="Prefix";Value="dada"} @{Name="Suffix";Value=".json"} ) } } } Write-S3BucketNotification -BucketName ssm-editor -LambdaFunctionConfiguration $firstLambdaConfig,$secondlambdaConfig
-
For API details, see PutBucketNotificationConfiguration
in Amazon Tools for PowerShell Cmdlet Reference.
-
For a complete list of Amazon SDK developer guides and code examples, see Using this service with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.