forked from awsdocs/aws-doc-sdk-examples
/
PublishMessage.go
67 lines (56 loc) · 1.98 KB
/
PublishMessage.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[sns.go.publish_message]
package main
// snippet-start:[sns.go.publish_message.imports]
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/aws/aws-sdk-go/service/sns/snsiface"
)
// snippet-end:[sns.go.publish_message.imports]
// PublishMessage publishes a message to an Amazon SNS topic
// Inputs:
// svc is an Amazon SNS service object
// msg is the message to publish
// topicARN is the Amazon Resource Name (ARN) of the topic to publish through
// Output:
// If success, information about the publication and nil
// Otherwise, nil and an error from the call to Publish
func PublishMessage(svc snsiface.SNSAPI, msg, topicARN *string) (*sns.PublishOutput, error) {
// snippet-start:[sns.go.publish_message.call]
result, err := svc.Publish(&sns.PublishInput{
Message: msg,
TopicArn: topicARN,
})
// snippet-end:[sns.go.publish_message.call]
return result, err
}
func main() {
// snippet-start:[sns.go.publish_message.args]
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
}
// snippet-end:[sns.go.publish_message.args]
// snippet-start:[sns.go.publish_message.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := sns.New(sess)
// snippet-end:[sns.go.publish_message.session]
result, err := PublishMessage(svc, msg, topicARN)
if err != nil {
fmt.Println("Got an error publishing the message:")
fmt.Println(err)
return
}
fmt.Println("Message ID: " + *result.MessageId)
}
// snippet-end:[sns.go.publish_message]