AWSDocAWS SDKGitHub サンプルリポジトリには、さらに多くの SDK サンプルがあります
翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
Amazon SNSSDK for Go V2
次のコード例は、Amazon SNS でアクションを実行する方法を示しています。AWS SDK for Go
「アクション」は、個々のサービス関数の呼び出し方法を示すコードの抜粋です。
「シナリオ」は、同じサービス内で複数の関数を呼び出して、特定のタスクを実行する方法を示すコード例です。
それぞれの例にはGitHub、へのリンクがあり、コンテキストでコードを設定および実行する方法についての説明が記載されています。
トピック
アクション
次のコード例は、Amazon SNS トピックを作成する方法を示しています。
- SDK for Go V2
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 package main import ( "context" "flag" "fmt" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/sns" ) // SNSCreateTopicAPI defines the interface for the CreateTopic function. // We use this interface to test the function using a mocked service. type SNSCreateTopicAPI interface { CreateTopic(ctx context.Context, params *sns.CreateTopicInput, optFns ...func(*sns.Options)) (*sns.CreateTopicOutput, error) } // MakeTopic creates an Amazon Simple Notification Service (Amazon SNS) topic. // Inputs: // c is the context of the method call, which includes the AWS Region. // api is the interface that defines the method call. // input defines the input arguments to the service call. // Output: // If success, a CreateTopicOutput object containing the result of the service call and nil. // Otherwise, nil and an error from the call to CreateTopic. func MakeTopic(c context.Context, api SNSCreateTopicAPI, input *sns.CreateTopicInput) (*sns.CreateTopicOutput, error) { return api.CreateTopic(c, input) } func main() { topic := flag.String("t", "", "The name of the topic") flag.Parse() if *topic == "" { fmt.Println("You must supply the name of the topic") fmt.Println("-t TOPIC") return } cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { panic("configuration error, " + err.Error()) } client := sns.NewFromConfig(cfg) input := &sns.CreateTopicInput{ Name: topic, } results, err := MakeTopic(context.TODO(), client, input) if err != nil { fmt.Println(err.Error()) return } fmt.Println(*results.TopicArn) }
-
API の詳細については、AWS SDK for GoAPI CreateTopic
リファレンスのを参照してください。
-
次のコード例は、Amazon SNS トピックのサブスクライバーのリストを取得する方法を示しています。
- SDK for Go V2
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 package main import ( "context" "fmt" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/sns" ) // SNSListSubscriptionsAPI defines the interface for the ListSubscriptions function. // We use this interface to test the function using a mocked service. type SNSListSubscriptionsAPI interface { ListSubscriptions(ctx context.Context, params *sns.ListSubscriptionsInput, optFns ...func(*sns.Options)) (*sns.ListSubscriptionsOutput, error) } // GetSubscriptions retrieves a list of your Amazon Simple Notification Service (Amazon SNS) subscriptions. // Inputs: // c is the context of the method call, which includes the AWS Region. // api is the interface that defines the method call. // input defines the input arguments to the service call. // Output: // If success, a ListSubscriptionsOutput object containing the result of the service call and nil. // Otherwise, nil and an error from the call to ListSubscriptions. func GetSubscriptions(c context.Context, api SNSListSubscriptionsAPI, input *sns.ListSubscriptionsInput) (*sns.ListSubscriptionsOutput, error) { return api.ListSubscriptions(c, input) } func main() { cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { panic("configuration error, " + err.Error()) } client := sns.NewFromConfig(cfg) input := &sns.ListSubscriptionsInput{} result, err := GetSubscriptions(context.TODO(), client, input) if err != nil { fmt.Println("Got an error retrieving the subscriptions:") fmt.Println(err) return } fmt.Println("Topic ARN") fmt.Println("Subscription ARN") fmt.Println("-------------------------") for _, s := range result.Subscriptions { fmt.Println(*s.TopicArn) fmt.Println(*s.SubscriptionArn) fmt.Println("") } }
-
API の詳細については、AWS SDK for GoAPI ListSubscriptions
リファレンスのを参照してください。
-
次のコード例は、Amazon SNS トピックを一覧表示する方法を示しています。
- SDK for Go V2
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 package main import ( "context" "fmt" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/sns" ) // SNSListTopicsAPI defines the interface for the ListTopics function. // We use this interface to test the function using a mocked service. type SNSListTopicsAPI interface { ListTopics(ctx context.Context, params *sns.ListTopicsInput, optFns ...func(*sns.Options)) (*sns.ListTopicsOutput, error) } // GetTopics retrieves information about the Amazon Simple Notification Service (Amazon SNS) topics // Inputs: // c is the context of the method call, which includes the Region // api is the interface that defines the method call // input defines the input arguments to the service call. // Output: // If success, a ListTopicsOutput object containing the result of the service call and nil // Otherwise, nil and an error from the call to ListTopics func GetTopics(c context.Context, api SNSListTopicsAPI, input *sns.ListTopicsInput) (*sns.ListTopicsOutput, error) { return api.ListTopics(c, input) } func main() { cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { panic("configuration error, " + err.Error()) } client := sns.NewFromConfig(cfg) input := &sns.ListTopicsInput{} results, err := GetTopics(context.TODO(), client, input) if err != nil { fmt.Println("Got an error retrieving information about the SNS topics:") fmt.Println(err) return } for _, t := range results.Topics { fmt.Println(*t.TopicArn) } }
-
API の詳細については、AWS SDK for GoAPI ListTopics
リファレンスのを参照してください。
-
次のコード例は、Amazon SNS トピックにメッセージを発行する方法を示しています。
- SDK for Go V2
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 package main import ( "context" "flag" "fmt" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/sns" ) // SNSPublishAPI defines the interface for the Publish function. // We use this interface to test the function using a mocked service. type SNSPublishAPI interface { Publish(ctx context.Context, params *sns.PublishInput, optFns ...func(*sns.Options)) (*sns.PublishOutput, error) } // PublishMessage publishes a message to an Amazon Simple Notification Service (Amazon SNS) topic // Inputs: // c is the context of the method call, which includes the Region // api is the interface that defines the method call // input defines the input arguments to the service call. // Output: // If success, a PublishOutput object containing the result of the service call and nil // Otherwise, nil and an error from the call to Publish func PublishMessage(c context.Context, api SNSPublishAPI, input *sns.PublishInput) (*sns.PublishOutput, error) { return api.Publish(c, input) } func main() { msg := flag.String("m", "", "The message to send to the subscribed users of the topic") topicARN := flag.String("t", "", "The ARN of the topic to which the user subscribes") flag.Parse() if *msg == "" || *topicARN == "" { fmt.Println("You must supply a message and topic ARN") fmt.Println("-m MESSAGE -t TOPIC-ARN") return } cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { panic("configuration error, " + err.Error()) } client := sns.NewFromConfig(cfg) input := &sns.PublishInput{ Message: msg, TopicArn: topicARN, } result, err := PublishMessage(context.TODO(), client, input) if err != nil { fmt.Println("Got an error publishing the message:") fmt.Println(err) return } fmt.Println("Message ID: " + *result.MessageId) }
-
API の詳細については、AWS SDK for GoAPI リファレンスの「発行
」を参照してください。
-
次のコード例は、Amazon SNS トピックに E メールアドレスをサブスクライブする方法を示しています。
- SDK for Go V2
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 package main import ( "context" "flag" "fmt" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/sns" ) // SNSSubscribeAPI defines the interface for the Subscribe function. // We use this interface to test the function using a mocked service. type SNSSubscribeAPI interface { Subscribe(ctx context.Context, params *sns.SubscribeInput, optFns ...func(*sns.Options)) (*sns.SubscribeOutput, error) } // SubscribeTopic subscribes a user to an Amazon Simple Notification Service (Amazon SNS) topic by their email address // Inputs: // c is the context of the method call, which includes the Region // api is the interface that defines the method call // input defines the input arguments to the service call. // Output: // If success, a SubscribeOutput object containing the result of the service call and nil // Otherwise, nil and an error from the call to Subscribe func SubscribeTopic(c context.Context, api SNSSubscribeAPI, input *sns.SubscribeInput) (*sns.SubscribeOutput, error) { return api.Subscribe(c, input) } func main() { email := flag.String("e", "", "The email address of the user subscribing to the topic") topicARN := flag.String("t", "", "The ARN of the topic to which the user subscribes") flag.Parse() if *email == "" || *topicARN == "" { fmt.Println("You must supply an email address and topic ARN") fmt.Println("-e EMAIL -t TOPIC-ARN") return } cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { panic("configuration error, " + err.Error()) } client := sns.NewFromConfig(cfg) input := &sns.SubscribeInput{ Endpoint: email, Protocol: aws.String("email"), ReturnSubscriptionArn: true, // Return the ARN, even if user has yet to confirm TopicArn: topicARN, } result, err := SubscribeTopic(context.TODO(), client, input) if err != nil { fmt.Println("Got an error subscribing to the topic:") fmt.Println(err) return } fmt.Println(*result.SubscriptionArn) }
-
API の詳細については、AWS SDK for Go API リファレンスの「Subscribe
」を参照してください。
-