ruby/example_code/s3/s3-ruby-example-add-notification.rb
# Prerequisites:
#
# - An S3 bucket.
# - For an event notification to AWS Lambda, a Lambda function.
# - For an event notification to Amazon Simple Notification Service
#   (Amazon SNS), an SNS topic.
# - For an event notification to Amazon Simple Queue Service
#   (Amazon SQS), an SQS queue.
#
# @param s3_client [Aws::S3::Client] An initialized S3 client.
# @param bucket_name [String] The name of the bucket.
# @param events [Array] The S3 events to notify on.
# @param send_to_type [String] The type of AWS resource to notify. Allowed
#   values include 'lambda' for Lambda, 'sns' for SNS, and 'sqs' for SQS.
# @param resource_arn [String] The Amazon Resource Name (ARN) of the
#   AWS resource.
# @return [Boolean] true if the bucket notification configuration was set;
#   otherwise, false.
# @example
#   exit 1 unless bucket_notification_configuration_set?(
#     Aws::S3::Client.new(region: 'us-west-2'),
#     'doc-example-bucket',
#     ['s3:ObjectCreated:*'],
#     'sns',
#     'arn:aws:sns:us-west-2:111111111111:my-topic'
#   )

An S3 bucket.
For an event notification to AWS Lambda, a Lambda function.
For an event notification to Amazon Simple Notification Service
(Amazon SNS), an SNS topic.
For an event notification to Amazon Simple Queue Service
(Amazon SQS), an SQS queue.
An S3 bucket . For an event notification to AWS Lambda a Lambda function . For an event notification to Amazon Simple Notification Service ( Amazon SNS ) an SNS topic . For an event notification to Amazon Simple Queue Service ( Amazon SQS ) an SQS queue .
def bucket_notification_configuration_set?(
  s3_client,
  bucket_name,
  events,
  send_to_type,
  resource_arn
)
  case send_to_type
  when 'lambda'
    s3_client.put_bucket_notification_configuration(
      bucket: bucket_name,
      notification_configuration: {
        lambda_function_configurations: [
          {
            lambda_function_arn: resource_arn,
            events: events
          }
        ]
      }
    )
  when 'sns'
    s3_client.put_bucket_notification_configuration(
      bucket: bucket_name,
      notification_configuration: {
        topic_configurations: [
          {
            topic_arn: resource_arn,
            events: events
          }
        ]
      }
    )
  when 'sqs'
    s3_client.put_bucket_notification_configuration(
      bucket: bucket_name,
      notification_configuration: {
        queue_configurations: [
          {
            queue_arn: resource_arn,
            events: events
          }
        ]
      }
    )
  else
    puts 'Error setting bucket notification configuration: ' \
      "Cannot determine send-to type. Must be 'lambda', 'sns', or 'sqs'."
    return false
  end
  return true
rescue StandardError => e
  puts "Error setting bucket notification configuration: #{e.message}"
  return false
end
def bucket_notification_configuration_set? ( s3_client , bucket_name , events , send_to_type , resource_arn ) case send_to_type when 'lambda' s3_client . put_bucket_notification_configuration ( bucket : bucket_name , notification_configuration : { lambda_function_configurations : [ { lambda_function_arn : resource_arn , events : events } ] } ) when 'sns' s3_client . put_bucket_notification_configuration ( bucket : bucket_name , notification_configuration : { topic_configurations : [ { topic_arn : resource_arn , events : events } ] } ) when 'sqs' s3_client . put_bucket_notification_configuration ( bucket : bucket_name , notification_configuration : { queue_configurations : [ { queue_arn : resource_arn , events : events } ] } ) else puts 'Error setting bucket notification configuration: ' "Cannot determine send-to type. Must be 'lambda', 'sns', or 'sqs'." return false end return true rescue StandardError => e puts "Error setting bucket notification configuration: #{e.message}" return false end
Prerequisites:
 An S3 bucket.
Prerequisites : An S3 bucket .
true if the bucket notification configuration was set; otherwise, false.
true if the bucket notification configuration was set ; otherwise false .
Boolean
s3_client
An initialized S3 client.
An initialized S3 client .
bucket_name
The name of the bucket.
The name of the bucket .
events
The S3 events to notify on.
The S3 events to notify on .
send_to_type
The type of AWS resource to notify. Allowed values include 'lambda' for Lambda, 'sns' for SNS, and 'sqs' for SQS.
The type of AWS resource to notify . Allowed values include ' lambda ' for Lambda ' sns ' for SNS and ' sqs ' for SQS .
resource_arn
The Amazon Resource Name (ARN) of the AWS resource.
The Amazon Resource Name ( ARN ) of the AWS resource .
example
exit 1 unless bucket_notification_configuration_set?( Aws::S3::Client.new(region: 'us-west-2'), 'doc-example-bucket', ['s3:ObjectCreated:*'], 'sns', 'arn:aws:sns:us-west-2:111111111111:my-topic' )
exit 1 unless bucket_notification_configuration_set? ( Aws :: S3 :: Client . new ( region : ' us - west - 2 ' ) ' doc - example - bucket ' [ ' s3 : ObjectCreated : * ' ] ' sns ' ' arn : aws : sns : us - west - 2 : 111111111111 : my - topic ' )