There are more AWS SDK examples available in the AWS Doc SDK Examples
Use PutBucketNotification
with a CLI
The following code examples show how to use PutBucketNotification
.
- CLI
-
- AWS CLI
-
The applies a notification configuration to a bucket named
amzn-s3-demo-bucket
:aws s3api put-bucket-notification --bucket
amzn-s3-demo-bucket
--notification-configurationfile://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:amzn-s3-demo-bucket", "Condition": { "ArnLike": { "aws:SourceArn": "arn:aws:s3:*:*:amzn-s3-demo-bucket" } } } ] }
-
For API details, see PutBucketNotification
in AWS CLI Command 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 amzn-s3-demo-bucket -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 amzn-s3-demo-bucket -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 amzn-s3-demo-bucket -LambdaFunctionConfiguration $firstLambdaConfig,$secondlambdaConfig
-
For API details, see PutBucketNotification in AWS Tools for PowerShell Cmdlet Reference.
-