AWS SDK for Ruby
Developer Guide

Triggering a Notification When an Item is Added to an Amazon S3 Bucket

You can trigger a notification when there is a change in the objects in a bucket. These changes include:

  • When an object is added to the bucket

  • When an object is removed from the bucket

  • When an object stored with Reduced Redundancy is lost

You can configure the service to send a notification to:

  • An Amazon SNS topic

  • An Amazon SQS queue

  • A AWS Lambda function

To create a bucket notification, use the following procedure.

After you do these steps, your application can respond to the information. For example, the Lambda topic Programming Model describes how to use the various programming languages that Lambda supports.

Enabling Amazon S3 to Send a Notification

Learn how to configure an Amazon SNS topic or Amazon SQS queue, or create a Lambda function so that Amazon S3 can send a notification to them.

Creating an Amazon S3 Bucket Notification

This example enables the Amazon S3 bucket my-bucket to send a notification to the following when an item is added to the bucket:

  • The Amazon SNS topic with the ARN my-topic-arn

  • The Amazon SQS queue with the ARN my-queue-arn

  • The Lambda function with the ARN my-function-arn

require 'aws-sdk-s3' # v2: require 'aws-sdk' req = {} req[:bucket] = bucket_name events = ['s3:ObjectCreated:*'] notification_configuration = {} # Add function lc = {} lc[:lambda_function_arn] = 'my-function-arn' lc[:events] = events lambda_configurations = [] lambda_configurations << lc notification_configuration[:lambda_function_configurations] = lambda_configurations # Add queue qc = {} qc[:queue_arn] = 'my-topic-arn' qc[:events] = events queue_configurations = [] queue_configurations << qc notification_configuration[:queue_configurations] = queue_configurations # Add topic tc = {} tc[:topic_arn] = 'my-topic-arn' tc[:events] = events topic_configurations = [] topic_configurations << tc notification_configuration[:topic_configurations] = topic_configurations req[:notification_configuration] = notification_configuration req[:use_accelerate_endpoint] = false s3 = Aws::S3::Client.new(region: 'us-west-2') s3.put_bucket_notification_configuration(req)