SDK for Ruby を使用したAmazon SNS の例 - AWSSDK コードサンプル

AWSDocAWS SDKGitHub サンプルリポジトリには、さらに多くの SDK サンプルがあります

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

SDK for Ruby を使用したAmazon SNS の例

次のコード例は、Amazon SNSAWS SDK for Ruby でアクションを実行する方法を示しています。

アクション」は、個々のサービス関数の呼び出し方法を示すコードの抜粋です。

シナリオ」は、同じサービス内で複数の関数を呼び出して、特定のタスクを実行する方法を示すコード例です。

それぞれの例にはGitHub、へのリンクがあり、コンテキストでコードを設定および実行する方法についての説明が記載されています。

トピック

アクション

次のコード例は、Amazon SNS トピックを作成する方法を示しています。

SDK for Ruby
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

require "aws-sdk-sns" # v2: require 'aws-sdk' def topic_created?(sns_client, topic_name) sns_client.create_topic(name: topic_name) rescue StandardError => e puts "Error while creating the topic named '#{topic_name}': #{e.message}" end # Full example call: def run_me topic_name = "TOPIC_NAME" region = "REGION" sns_client = Aws::SNS::Client.new(region: region) puts "Creating the topic '#{topic_name}'..." if topic_created?(sns_client, topic_name) puts "The topic was created." else puts "The topic was not created. Stopping program." exit 1 end end run_me if $PROGRAM_NAME == __FILE__
  • 詳細については、AWS SDK for Ruby デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for RubyAPI CreateTopicリファレンスのを参照してください

次のコード例は、Amazon SNS トピックのサブスクライバーのリストを取得する方法を示しています。

SDK for Ruby
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

require "aws-sdk-sns" # v2: require 'aws-sdk' def show_subscriptions?(sns_client, topic_arn) topic = sns_client.topic(topic_arn) topic.subscriptions.each do |s| puts s.attributes["Endpoint"] end rescue StandardError => e puts "Error while sending the message: #{e.message}" end def run_me topic_arn = "SNS_TOPIC_ARN" region = "REGION" sns_client = Aws::SNS::Resource.new(region: region) puts "Listing subscriptions to the topic." if show_subscriptions?(sns_client, topic_arn) else puts "There was an error. Stopping program." exit 1 end end run_me if $PROGRAM_NAME == __FILE__
  • 詳細については、AWS SDK for Ruby デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for RubyAPI ListSubscriptionsリファレンスのを参照してください

次のコード例は、Amazon SNS トピックを一覧表示する方法を示しています。

SDK for Ruby
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

require "aws-sdk-sns" # v2: require 'aws-sdk' def list_topics?(sns_client) sns_client.topics.each do |topic| puts topic.arn rescue StandardError => e puts "Error while listing the topics: #{e.message}" end end def run_me region = "REGION" sns_client = Aws::SNS::Resource.new(region: region) puts "Listing the topics." if list_topics?(sns_client) else puts "The bucket was not created. Stopping program." exit 1 end end run_me if $PROGRAM_NAME == __FILE__
  • 詳細については、AWS SDK for Ruby デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for RubyAPI ListTopicsリファレンスのを参照してください

次のコード例は、Amazon SNS トピックにメッセージを発行する方法を示しています。

SDK for Ruby
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

require "aws-sdk-sns" # v2: require 'aws-sdk' def message_sent?(sns_client, topic_arn, message) sns_client.publish(topic_arn: topic_arn, message: message) rescue StandardError => e puts "Error while sending the message: #{e.message}" end def run_me topic_arn = "SNS_TOPIC_ARN" region = "REGION" message = "MESSAGE" # The text of the message to send. sns_client = Aws::SNS::Client.new(region: region) puts "Message sending." if message_sent?(sns_client, topic_arn, message) puts "The message was sent." else puts "The message was not sent. Stopping program." exit 1 end end run_me if $PROGRAM_NAME == __FILE__
  • 詳細については、AWS SDK for Ruby デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for RubyAPI リファレンスの「発行」を参照してください。

次のコード例は、Amazon SNS トピック属性を設定する方法を示しています。

SDK for Ruby
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

require "aws-sdk-sns" # v2: require 'aws-sdk' policy = '{ "Version":"2008-10-17", "Id":"__default_policy_ID", "Statement":[{ "Sid":"__default_statement_ID", "Effect":"Allow", "Principal":{ "AWS":"*" }, "Action":["SNS:Publish"], "Resource":"' + MY_TOPIC_ARN + '", "Condition":{ "ArnEquals":{ "AWS:SourceArn":"' + MY_RESOURCE_ARN + '"} } }] }' # Replace us-west-2 with the AWS Region you're using for Amazon SNS. sns = Aws::SNS::Resource.new(region: "REGION") # Get topic by ARN topic = sns.topic() # Add policy to topic topic.set_attributes({ attribute_name: "POLICY_NAME", attribute_value: policy })
  • 詳細については、AWS SDK for Ruby デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for RubyAPI SetTopicAttributesリファレンスのを参照してください

次のコード例は、Amazon SNS トピックに E メールアドレスをサブスクライブする方法を示しています。

SDK for Ruby
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

require "aws-sdk-sns" # v2: require 'aws-sdk' def subscription_created?(sns_client, topic_arn, protocol, endpoint) sns_client.subscribe(topic_arn: topic_arn, protocol: protocol, endpoint: endpoint) rescue StandardError => e puts "Error while creating the subscription: #{e.message}" end # Full example call: def run_me protocol = "email" endpoint = "EMAIL_ADDRESS" topic_arn = "TOPIC_ARN" region = "REGION" sns_client = Aws::SNS::Client.new(region: region) puts "Creating the subscription." if subscription_created?(sns_client, topic_arn, protocol, endpoint) puts "The subscriptions was created." else puts "The subscription was not created. Stopping program." exit 1 end end run_me if $PROGRAM_NAME == __FILE__
  • 詳細については、AWS SDK for Ruby デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for Ruby API リファレンスの「Subscribe」を参照してください。