douzhouqin6223 2018-09-06 08:26
浏览 86

配置aws-sdk-go与localstack的SQS进行交互

I need some help trying to integrate aws-sdk-go with localstack to access SQS service.

I tried something like:

result, err := q.Client.SendMessage(&sqs.SendMessageInput{
    MessageAttributes: map[string]*sqs.MessageAttributeValue{
        "JobName": &sqs.MessageAttributeValue{
            DataType:    aws.String("String"),
            StringValue: aws.String(jobName),
        },
    },
    MessageBody: aws.String(messageBody),
    QueueUrl:    &q.URL,
})

if err != nil {
    return "", err
}

With an initialization like:

type Queue struct {
    Client sqsiface.SQSAPI
    URL    string
}

var q Queue

    func init() {
      sess := session.Must(session.NewSessionWithOptions(session.Options{
        SharedConfigState: session.SharedConfigEnable,
    }))

    q = Queue{
        Client: sqs.New(sess),
        URL:    viper.GetString("queue.sqs.url"),
    }
}

How can I configure the SDK to access localstack's SQS?

  • 写回答

1条回答 默认 最新

  • douzha6055 2018-09-13 09:06
    关注

    Let's suppose you have localstack up and running with default configuration so that SQS will be running on port :4576, therefore, it will be accessible at http://localhost:4576.

    Let's write an example where we send a message on SQS after creating the Session and the Queue:

    package main
    
    import (
    
        "encoding/json"
        "log"
    
        "github.com/aws/aws-sdk-go/aws"
        "github.com/aws/aws-sdk-go/aws/session"
        "github.com/aws/aws-sdk-go/service/sqs"
    )
    
    const (
    
        // Region represents aws region
        Region      = "eu-central-1"
        // SQSEndpoint represents the sqs endpoint (in this case points to localstack's SQS)
        SQSEndpoint = "http://localhost:4576"
        // QueueName represents queue's name
        QueueName   = "some_queue"
        // QueueURL represents queue's URL. You can also dynamically retrieve it with sqs.GetQueueUrl
        QueueURL    = "http://localhost:4576/queue/some_queue"
    )
    
    // Message is an example struct representing a message to be sent
    type Message struct {
        SomeParam string `json:"some_param"`
    }
    
    func main() {
    
        // Initialize a new Config
        cfg := new(aws.Config)
        // Setting up region
        cfg.Region = aws.String(Region)
        // Setting up SQS Endpoint
        cfg.Endpoint = aws.String(SQSEndpoint)
    
        // Initialize new AWS Session
        awsSvc, err := session.NewSession(awsSvc)
        if err != nil {
            log.Fatal(err)
        }
    
        // Initialize new SQS Session
        sqsSvc := sqs.New(awsSvc)
    
        // Create a new queue input
        createQIn := new(sqs.CreateQueueInput)
        createQIn = out.SetQueueName(QueueName)
    
        // Create the queue
        if _, err := sqsSvc.CreateQueue(createQIn); err != nil {
            log.Fatal(err)
        }
    
        // Message Input
        message := Message {
            SomeParam : "some_val"
        }
    
        // Build SendMessage Input
        sendMsgIn:= new(sqs.SendMessageInput)
    
        // Marshal message to json
        b, err:= json.Marshal(input)
        if err != nil {
            log.Fatal(err)
        }
    
        // Set both Body and URL
        sendMsgIn = sendMsgIn.SetMessageBody(b).SetQueueUrl(QueueURL)
    
        // Finally send message
        if _, err := sqsSvc.SendMessage(sendMsgIn); err != nil {
            log.Fatal(err)
        }
    
    }
    

    Anyways, I realized a small utility library for this SDK with some useful methods aws-sdk-go-bindings.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算