I'm having issue with sending a message to SNS with the AWS Go SDK. Documentation for the Publish function is a little bit obscure.
My piece of code is:
package main
import (
"encoding/json"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/sns"
"github.com/aws/aws-sdk-go/aws"
"log"
)
type Person struct {
Name string `json:"name"`
}
func main() {
cfg, _ := external.LoadDefaultAWSConfig()
snsClient := sns.New(cfg)
person := Person{
Name:"ok",
}
jsonStr, _ := json.Marshal(person)
req := snsClient.PublishRequest(&sns.PublishInput{
TopicArn: aws.String("arn:aws:sns:us-east-1:*****:ok"),
Message: aws.String(string(jsonStr)),
MessageStructure: aws.String("json"),
MessageAttributes: map[string]sns.MessageAttributeValue{
"default": {
DataType: aws.String("String"),
StringValue: aws.String(string(jsonStr)),
},
},
})
res, err := req.Send()
if err != nil {
log.Fatal(err)
}
log.Print(res)
}
When I launch this code I receive the following message :
2019/01/24 20:14:24 InvalidParameter: Invalid parameter: Message Structure - No default entry in JSON message body
status code: 400, request id: 55940de1-9645-5485-96c5-592586957ce8
exit status 1
Maybe someone can help me with that ?
Thanks
I've found a solution to my issue.
package main
import (
"encoding/json"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/sns"
"github.com/aws/aws-sdk-go-v2/aws"
"log"
)
type Message struct {
Default string `json:"default"`
}
type Person struct {
Name string `json:"name"`
}
func main() {
cfg, _ := external.LoadDefaultAWSConfig()
snsClient := sns.New(cfg)
person := Person{
Name: "Felix Kjellberg",
}
personStr, _ := json.Marshal(person)
message := Message{
Default: string(personStr),
}
messageBytes, _ := json.Marshal(message)
messageStr := string(messageBytes)
req := snsClient.PublishRequest(&sns.PublishInput{
TopicArn: aws.String("arn:aws:sns:us-east-1:*****:ok"),
Message: aws.String(messageStr),
MessageStructure: aws.String("json"),
})
res, err := req.Send()
if err != nil {log.Fatal(err)
}
log.Print(res)
}
Some encoding inception was needed
You have to add a "default" field to your json payload for subscribers that can't consume your message payload. Take a look at this (towards the bottom): https://docs.aws.amazon.com/sns/latest/dg/mobile-push-send-custommessage.html
Related
I am trying to connect my local MinIO instance running in docker. I have written a simple Go program to do the work. See following:
package main
import (
"bytes"
"context"
"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/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"log"
)
func main() {
endpoint := "localhost:9000"
accessKeyID := "1mVyDeEcrRU7qQ7h"
secretAccessKey := "vNi9SNfcHNxfRDETSvkeFLO210mzu7ee"
useSSL := false
awsSession := session.Must(session.NewSession(&aws.Config{
Region: aws.String("ap-south-1"),
Credentials: credentials.NewStaticCredentialsFromCreds(credentials.Value{
AccessKeyID: accessKeyID,
SecretAccessKey: secretAccessKey,
SessionToken: "",
}),
Endpoint: aws.String(endpoint),
DisableSSL: aws.Bool(!useSSL),
}))
ctx := context.Background()
file, err := downloadFileFromS3(ctx, awsSession, "mybucket", "data.txt")
if err != nil {
log.Fatal(err)
}
reader := bytes.NewReader(file)
println(reader)
}
func downloadFileFromS3(ctx context.Context, sess *session.Session, bucketName string, fileName string) ([]byte, error) {
downloader := s3manager.NewDownloader(sess)
buff := &aws.WriteAtBuffer{}
_, err := downloader.DownloadWithContext(ctx, buff,
&s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(fileName),
})
if err != nil {
return nil, err
}
return buff.Bytes(), nil
}
But this fails to do the job. Check error below:
GOROOT=/usr/local/go #gosetup
GOPATH=/Users/kuldeep/go #gosetup
/usr/local/go/bin/go build -o /private/var/folders/29/zkqqp9_d0fscymp5ph4rscl40000gp/T/GoLand/___2go_build_minio_client_example minio-client-example #gosetup
/private/var/folders/29/zkqqp9_d0fscymp5ph4rscl40000gp/T/GoLand/___2go_build_minio_client_example
2023/01/16 22:37:52 RequestError: send request failed
caused by: Get "http://mybucket.localhost:9000/data.txt": dial tcp: lookup mybucket.localhost: no such host
Process finished with the exit code 1
What should I do to make it work?
Trying to create a Lambda to interact with my DynamoDB.
This specific Lambda is to put/write an item to the DB:
package main
import (
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
type Item struct {
Email string `json:"email"`
Password string `json:"password"`
Rname string `json:"rname"`
}
func Put() error {
// Create a session - London Region
session, err := session.NewSession(&aws.Config{
Region: aws.String("eu-west-2")},
)
if err != nil {
fmt.Println(err)
}
svc := dynamodb.New(session)
// Create instance of Item Struct
item := Item{
Email: "123#mail.com",
Password: "12345678",
Rname: "abcde",
}
// Marshall Item
av, err := dynamodbattribute.MarshalMap(item)
if err != nil {
fmt.Println("Got error marshalling map:")
fmt.Println(err)
}
// Create Item
input := &dynamodb.PutItemInput{
Item: av,
TableName: aws.String("accountsTable"),
}
_, err = svc.PutItem(input)
if err != nil {
fmt.Println("Got error calling PutItem:")
fmt.Println(err)
}
return err
}
func main() {
lambda.Start(Put())
}
However getting the error:
{
"errorMessage": "handler is nil",
"errorType": "errorString"
}
I have changed the handler in run time settings to main too so don't think that would be the issue.
Building with:
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -a main.go
and putting the zip of the executable into AWS via console (no IAC's)
Any help would be greatly appreciated to resolve this error! Thanks.
You need to pass function handle not function result to lambda.Start
Please update your main function with👇
func main() {
lambda.Start(Put)
}
I have the following struct:
type ProcessedRecords struct {
CustIndividualID string `json:"individual id"`
Household string `json:"Household"`
}
And I have a slice of many structs that share this value. I'm trying to submit them using the PutRecordBatch operation from the AWS SDK:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/firehose"
)
type ProcessedRecords struct {
CustIndividualID string `json:"individual id"`
Household string `json:"Household"`
}
func main() {
submitToFirehose(recordList)
}
func submitToFirehose(records []ProcessedRecords) {
streamName := "processed-stream"
sess := session.Must(session.NewSession())
// Create a Firehose client with additional configuration
firehoseService := firehose.New(sess, aws.NewConfig().WithRegion("us-east-1"))
recordsBatchInput := &firehose.PutRecordBatchInput{}
recordsBatchInput = recordsBatchInput.SetDeliveryStreamName(streamName)
recordsInput := []*firehose.Record{}
for i := 0; i < len(records); i++ {
if len(recordsInput) == 500 {
recordsBatchInput = recordsBatchInput.SetRecords(recordsInput)
resp, err := firehoseService.PutRecordBatch(recordsBatchInput)
if err != nil {
fmt.Printf("PutRecordBatch err: %v\n", err)
} else {
fmt.Printf("FailedPuts: %v\n", *resp.FailedPutCount)
}
recordsInput = []*firehose.Record{}
}
b, err := json.Marshal(records[i])
if err != nil {
log.Printf("Error: %v", err)
}
record := &firehose.Record{Data: b}
recordsInput = append(recordsInput, record)
}
}
This seems to work and it would appear that my Glue backend is setup correctly, however CustIndividualID is not being written to S3. I suspect it's because it's reading the json:"individual id" as the column name and not the CustIndividualID.
this is a problem because glue tables can't have spaces in the column name. What am I doing wrong?
I am using Gomock https://godoc.org/github.com/golang/mock and mockgen
The Source code for this test is:
package sqs
import (
"fmt"
"log"
"os"
"runtime"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/aws/aws-sdk-go/service/sqs/sqsiface"
)
var sess *session.Session
var svc *sqs.SQS
var queueURL string
func init() {
// Setting the runtime to run with max CPUs available
runtime.GOMAXPROCS(runtime.NumCPU())
sess = session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc = sqs.New(sess)
queueURL = os.Getenv("QUEUE_URL")
}
type Poller interface {
Poll(chan bool)
}
// NewPoller is a factory to create a Poller object
func NewPoller(msgr Messenger) Poller {
p := &poller{
m: msgr,
}
return p
}
type poller struct {
m Messenger
}
func (p *poller) Poll(done chan bool) {
sqsMsgCh := make(chan *sqs.Message, 100)
for {
messages, err := p.m.GetMessage()
if err != nil {
log.Printf("error when getting message")
if len(messages) == 0 {
// Stop the system
log.Printf("I am here")
done <- true
}
}
for _, msg := range messages {
sqsMsgCh <- msg
}
}
}
type Messenger interface {
GetMessage() ([]*sqs.Message, error)
}
func NewMessenger() Messenger {
return &messenger{
s: svc,
}
}
type messenger struct {
s sqsiface.SQSAPI
}
func (m *messenger) GetMessage() ([]*sqs.Message, error) {
result, err := m.s.ReceiveMessage(&sqs.ReceiveMessageInput{
AttributeNames: []*string{
aws.String(sqs.MessageSystemAttributeNameSentTimestamp),
},
MessageAttributeNames: []*string{
aws.String(sqs.QueueAttributeNameAll),
},
QueueUrl: aws.String(queueURL),
MaxNumberOfMessages: aws.Int64(10),
VisibilityTimeout: aws.Int64(36000), // 10 hours
WaitTimeSeconds: aws.Int64(0),
})
if err != nil {
fmt.Println("Error", err)
return nil, err
}
msgs := result.Messages
if len(msgs) == 0 {
fmt.Println("Received no messages")
return msgs, err
}
return msgs, nil
}
The test case for this Source file is here:
package sqs
import (
"errors"
"testing"
"path_to_the_mocks_package/mocks"
"github.com/golang/mock/gomock"
"github.com/aws/aws-sdk-go/service/sqs"
)
func TestPollWhenNoMessageOnQueue(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
msgr := mocks.NewMockMessenger(mockCtrl)
mq := make([]*sqs.Message, 1)
err := errors.New("Mock Error")
// msgr.EXPECT().GetMessage().Return(mq, err) //.Times(1)
// msgr.GetMessage().Return(mq, err) //.Times(1)
msgr.EXPECT().GetMessage().Return(mq, err)
p := NewPoller(msgr)
done := make(chan bool)
go p.Poll(done)
<-done
t.Logf("Successfully done: %v", done)
}
When I run the tests I am getting the following error:
sqs\controller.go:150: Unexpected call to
*mocks.MockMessenger.GetMessage([]) at path_to_mocks_package/mocks/mock_messenger.go:38 because: Expected
call at path_to_sqs_package/sqs/sqs_test.go:35 has already been called
the max number of times. FAIL
If I write my own mock as follows the test case executes successfully:
type mockMessenger struct {
mock.Mock
}
func (m *mockMessenger) GetMessage() ([]*sqs.Message, error) {
msgs := make([]*sqs.Message, 0)
err := errors.New("Error")
return msgs, err
}
You are implicitly telling gomock that you only expect a single call.
msgr.EXPECT().GetMessage().Return(mq, err)
Adding a number of Times to the mock, allows you to return those values more than once.
msgr.EXPECT().GetMessage().Return(mq, err).AnyTimes()
For more details please read the gomock's AnyTimes documentation.
Here's a minification of using the stackdriver trace go client package.
It seems like this trivial example should work but it produces an error.
package main
import (
"context"
"flag"
"log"
"net/http"
"cloud.google.com/go/trace"
"github.com/davecgh/go-spew/spew"
"google.golang.org/api/option"
)
var (
projectID = flag.String("projectID", "", "projcect id")
serviceAccountKey = flag.String("serviceAccountKey", "", "path to serviceacount json")
)
func main() {
flag.Parse()
mux := http.NewServeMux()
log.Fatalln(http.ListenAndServe(":9000", Trace(*projectID, *serviceAccountKey, mux)))
}
func Trace(projectID string, keyPath string, h http.Handler) http.HandlerFunc {
client, err := trace.NewClient(context.Background(), projectID, option.WithServiceAccountFile(keyPath))
if err != nil {
panic(err)
}
return func(w http.ResponseWriter, r *http.Request) {
s := client.SpanFromRequest(r)
defer func() { err := s.FinishWait(); spew.Dump(err) }()
h.ServeHTTP(w, r)
}
}
I'm running this as:
$ teststackdrivertrace -projectID ${GCLOUD_PROJECT_ID} -serviceAccountKey ${PATH_TO_SERVICEACCOUNT_JSON}
and curling it produces:
$ curl -s --header "X-Cloud-Trace-Context: 205445aa7843bc8bf206b120001000/0;o=1" localhost:9000
$ (*googleapi.Error)(0xc4203a2c80)(googleapi: Error 400: Request contains an invalid argument., badRequest)
What am I missing?
My traceId was 30 bytes long not 32. I took the example curl from https://cloud.google.com/trace/docs/faq which is also only 30 bytes.