简体   繁体   中英

Subscribe to an SNS topic and / or SQS queue in golang?

I know how to do this in java, but I just can't figure it out in Go at all.

All I want to do, is have a way to detect that an item got created in an S3 bucket, then have that trigger an SNS topic, which then notifies me of the file location in S3.

Has anybody got a working example of how I can do the go side of this for subscribing to the SNS topic or the SNS queue if I need one? Because all I seem to be able to find is Java and Node. I can find publish examples for go, but they are of little use to my use case.

To use SNS you will need a simple HTTP/HTTPS endpoint to receive SNS notifications. Which is divided into two parts (Confirm the subscription and Processing messages from HTTP/HTTPS endpoint)

1. Confirm the subscription Do something as simple as this:

func confirmSubscription(subcribeURL string) {
    response, err := http.Get(subcribeURL)
    if err != nil {
        fmt.Printf("Unbale to confirm subscriptions")
    } else {
        fmt.Printf("Subscription Confirmed sucessfully. %d", response.StatusCode)
    }
}

2. Processing messages from HTTP/HTTPS endpoint

Parse the request's body, the documentations mentions how the body should be structured.

Sources:

https://docs.aws.amazon.com/sns/latest/dg/sns-http-https-endpoint-as-subscriber.html

https://github.com/viveksyngh/aws-sns-subscriber/blob/master/subscriber/subscriber.go

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM