6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS SDK for Go SNS コードサンプル

Last updated at Posted at 2019-05-31

概要

公式Doc
https://docs.aws.amazon.com/ja_jp/sdk-for-go/v1/developer-guide/using-sns-with-go-sdk.html

SNSと接続するインスタンスの生成

アクセスキーをローカルファイルから自動で読み込む場合

"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"

// ~/.aws/credentialsからアクセスキーを読み込む
sess := session.Must(session.NewSessionWithOptions(session.Options{
		SharedConfigState: session.SharedConfigEnable,
	}))
svc := sns.New(sess)

アクセスキーを明示する場合

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"

creds := credentials.NewStaticCredentials("AWS_ACCESS_KEY", "AWS_SECRET_ACCESS_KEY", "")
sess, err := session.NewSession(&aws.Config{
    Credentials: creds,
    Region:      aws.String("ap-northeast-1"),
})
if err != nil {
    fmt.Println(err)
}

svc := sns.New(sess)

すべてのトピックに関する情報を取得する

ListTopics()

func (c *SNS) ListTopics(input *ListTopicsInput) (*ListTopicsOutput, error)

result, _ := svc.ListTopics(nil)
for _, t := range result.Topics {
    fmt.Println(*t.TopicArn)
}

トピックの作成

CreateTopic()

func (c *SNS) CreateTopic(input *CreateTopicInput) (*CreateTopicOutput, error)

result, _ := svc.CreateTopic(&sns.CreateTopicInput{
    Name: aws.String("test-topic"),
})

fmt.Println(*result.TopicArn)

トピックのサブスクリプションに関する情報を取得する

ListSubscriptions()

func (c *SNS) ListSubscriptions(input *ListSubscriptionsInput) (*ListSubscriptionsOutput, error)

すべてのサブスクリプションを取得する

result := svc.ListSubscriptions(nil)

for _, s := range result.Subscriptions {
	fmt.Println(*s.Endpoint)
	fmt.Println(*s.Owner)
	fmt.Println(*s.SubscriptionArn)
	fmt.Println(*s.TopicArn)
}

ListSubscriptionsByTopic()

func (c *SNS) ListSubscriptionsByTopic(input *ListSubscriptionsByTopicInput) (*ListSubscriptionsByTopicOutput, error)

TopicArnからサブスクリプションを取得する

result, _ := svc.ListSubscriptionsByTopic(&sns.ListSubscriptionsByTopicInput{
		TopicArn: aws.String("arn:aws:sns:ap-northeast-1:1234567890:test-topic"),
	})

for _, s := range result.Subscriptions {
	fmt.Println(*s.Endpoint)
	fmt.Println(*s.Owner)
	fmt.Println(*s.SubscriptionArn)
	fmt.Println(*s.TopicArn)
}

トピックのサブスクリプションを作成

Subscribe()

func (c *SNS) Subscribe(input *SubscribeInput) (*SubscribeOutput, error)

eメールアドレスをトピックに登録してサブスクリプションを作成する

result, _ := svc.Subscribe(&sns.SubscribeInput{
	Endpoint:              aws.String("example@example.com"),
	Protocol:              aws.String("email"),
	ReturnSubscriptionArn: aws.Bool(true),
	TopicArn:              aws.String("arn:aws:sns:ap-northeast-1:1234567890:test-topic"),
})

fmt.Println(*result.SubscriptionArn)

トピックのサブスクライバーにメッセージを送信

Publish()

func (c *SNS) Publish(input *PublishInput) (*PublishOutput, error)

input := &sns.PublishInput{
	Message:  aws.String("Hello world!"),
	TopicArn: aws.String("arn:aws:sns:ap-northeast-1:1234567890:test-topic"),
}
result, _ := svc.Publish(input)

fmt.Println(result)
6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?