/
SnsPublish.go
47 lines (37 loc) · 1.13 KB
/
SnsPublish.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[sns.go.publish]
package main
import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
"flag"
"fmt"
"os"
)
func main() {
msgPtr := flag.String("m", "", "The message to send to the subscribed users of the topic")
topicPtr := flag.String("t", "", "The ARN of the topic to which the user subscribes")
flag.Parse()
if *msgPtr == "" || *topicPtr == "" {
fmt.Println("You must supply a message and topic ARN")
fmt.Println("Usage: go run SnsPublish.go -m MESSAGE -t TOPIC-ARN")
os.Exit(1)
}
// Initialize a session that the SDK will use to load
// credentials from the shared credentials file. (~/.aws/credentials).
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := sns.New(sess)
result, err := svc.Publish(&sns.PublishInput{
Message: msgPtr,
TopicArn: topicPtr,
})
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
fmt.Println(*result.MessageId)
}
// snippet-end:[sns.go.publish]