Select Git revision
-
James Page authoredJames Page authored
sqs.go 4.79 KiB
package sqs
/*Package sqs provides a simple interface to send messages to AWS SQS*/
import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"gitlab.com/uafrica/go-utils/s3"
"gitlab.com/uafrica/go-utils/string_utils"
"io/ioutil"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"gitlab.com/uafrica/go-utils/logs"
)
var sqsClient *sqs.SQS
const SQSMessageOnS3Key = "message-on-s3"
// Messenger sends an arbitrary message via SQS
type Messenger struct {
QueueName string
QueueURL string
Region string
S3Session *s3.SessionWithHelpers
S3BucketName string
MessageGroupID *string
RequestIDHeaderKey string
}
// NewSQSClient constructs a Messenger which sends messages to an SQS queue
// awsRegion - region that the queue was created
// awsQueue - name of the queue
// Note: Calling code needs SQS IAM permissions
func NewSQSClient(awsRegion string) (*sqs.SQS, error) {
// Make an AWS session
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{
Region: aws.String(awsRegion),
},
})
if err != nil {
return nil, err
}
// Create SQS service
sqsClient = sqs.New(sess)
return sqsClient, err
}
// SendSQSMessage sends a message to the queue associated with the messenger
// headers - string message attributes of the SQS message (see AWS SQS documentation)
// body - body of the SQS message (see AWS SQS documentation)
func (m *Messenger) SendSQSMessage(headers map[string]string, body string, currentRequestID *string, sqsType string) (string, error) {
msgAttrs := make(map[string]*sqs.MessageAttributeValue)
for key, val := range headers {
msgAttrs[key] = &sqs.MessageAttributeValue{
DataType: aws.String("String"),
StringValue: aws.String(val),
}
}
// Add request ID
if currentRequestID != nil {