ways-to-add-notification-config-to-bucket
Example Walkthrough: Configure a Bucket for Notifications (Message Destination: SNS Topic and SQS Queue)
Topics
- Walkthrough Summary
- Step 1: Create an Amazon SNS Topic
- Step 2: Create an Amazon SQS Queue
- Step 3: Add a Notification Configuration to Your Bucket
- Step 4: Test the Setup
Walkthrough Summary
In this example, you add a notification configuration on a bucket requesting Amazon S3 to do the following:
- Publish events of the
s3:ObjectCreated:*
type to an Amazon SQS queue. - Publish events of the
s3:ReducedRedundancyLostObject
type to an Amazon SNS topic.
For information about notification configuration, see Configuring Amazon S3 Event Notifications.
You can do all these steps using the console, without writing any code. In addition, code examples using AWS SDKs for Java and .NET are also provided to help you add notification configurations programmatically.
You do the following in this walkthrough:
Create an Amazon SNS topic.
Using the Amazon SNS console, you create an SNS topic and subscribe to the topic so that any events posted to it are delivered to you. You specify email as the communications protocol. After you create a topic, Amazon SNS sends an email. You must click a link in the email to confirm the topic subscription.
You attach an access policy to the topic to grant Amazon S3 permission to post messages.
Create an Amazon SQS queue.
Using the Amazon SQS console, you create an SQS queue. You can access any messages Amazon S3 sends to the queue programmatically. But for this walkthrough, you verify notification messages in the console.
You attach an access policy to the topic to grant Amazon S3 permission to post messages.
Add notification configuration to a bucket.
Step 1: Create an Amazon SNS Topic
Follow the steps to create and subscribe to an Amazon Simple Notification Service (Amazon SNS) topic.
Using Amazon SNS console create a topic. For instructions, see Create a Topic in the Amazon Simple Notification Service Developer Guide.
Subscribe to the topic. For this exercise, use email as the communications protocol. For instructions, see Subscribe to a Topic in the Amazon Simple Notification Service Developer Guide.
You will get email requesting you to confirm your subscription to the topic. Confirm the subscription.
Replace the access policy attached to the topic with the following policy. You must update the policy by providing your SNS topic Amazon Resource Name (ARN) and bucket name.
{"Version": "2008-10-17","Id": "example-ID","Statement": [{"Sid": "example-statement-ID","Effect": "Allow","Principal": {"AWS":"*"},"Action": ["SNS:Publish"],"Resource": "SNS-topic-ARN","Condition": {"ArnLike": { "aws:SourceArn": "arn:aws:s3:*:*:bucket-name" }}}]}Note the topic ARN.
The SNS topic you created is another resource in your AWS account, and it has a unique Amazon Resource Name (ARN). You will need this ARN in the next step. The ARN will be of the following format:
arn:aws:sns:aws-region:account-id:topic-name
Step 2: Create an Amazon SQS Queue
Follow the steps to create and subscribe to an Amazon Simple Queue Service (Amazon SQS) queue.
Using the Amazon SQS console, create a queue. For instructions, see Getting Started with Amazon SQS in the Amazon Simple Queue Service Developer Guide.
Replace the access policy attached to the queue with the following policy. (In the Amazon SQS console, select the queue, and in the Permissions tab, choose Edit Policy Document (Advanced).
{"Version": "2012-10-17","Id": "example-ID","Statement": [{"Sid": "example-statement-ID","Effect": "Allow","Principal": {"AWS":"*"},"Action": ["SQS:SendMessage"],"Resource": "SQS-queue-ARN","Condition": {"ArnLike": { "aws:SourceArn": "arn:aws:s3:*:*:bucket-name" }}}]}(Optional) If the Amazon SQS queue is server-side encryption (SSE) enabled, add the following policy to the associated symmetric customer managed AWS KMS CMK. You must add the policy to a customer managed CMK because the AWS managed CMK for Amazon SQS cannot be modified. For more information about using SSE for Amazon SQS with AWS KMS, see Protecting Data Using Server-Side Encryption (SSE) and AWS KMS.
{"Version": "2012-10-17","Id": "example-ID","Statement": [{"Sid": "example-statement-ID","Effect": "Allow","Principal": {"Service": "s3.amazonaws.com"},"Action": ["kms:GenerateDataKey","kms:Decrypt"],"Resource": "*"}]}Note the queue ARN.
The SQS queue you created is another resource in your AWS account, and it has a unique Amazon Resource Name (ARN). You will need this ARN in the next step. The ARN will be of the following format:
arn:aws:sqs:aws-region:account-id:queue-name
Step 3: Add a Notification Configuration to Your Bucket
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.
Step 3 (Option a): Enable Notifications on a Bucket Using the Console
Using the Amazon S3 console, add a notification configuration requesting Amazon S3 to:
- Publish events of the
s3:ObjectCreated:*
type to your Amazon SQS queue. - Publish events of the
s3:ReducedRedundancyLostObject
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 How Do I Enable and Configure Event Notifications for an S3 Bucket? in the Amazon Simple Storage Service Console User Guide.
Step 3 (Option b): Enable Notifications on a Bucket Using the AWS SDK for .NET
The following C# code example provides a complete code listing that adds a notification configuration to a bucket. You need to update the code and provide your bucket name and SNS topic ARN. For information about how to create and test a working sample, see Running the Amazon S3 .NET Code Examples.
Example
Step 3 (Option c): Enable Notifications on a Bucket Using the AWS SDK for Java
The following example shows how to add a notification configuration to a bucket. For instructions on creating and testing a working sample, see Testing the Amazon S3 Java Code Examples.
Example
Step 4: Test the Setup
Now you can test the setup by uploading an object to your bucket and verifying the event notification in the Amazon SQS console. For instructions, see Receiving a Message in the Amazon Simple Queue Service Developer Guide "Getting Started" section.