2018-10-31 08:11:19 +00:00
|
|
|
package aws_sqs
|
|
|
|
|
|
|
|
import (
|
2018-11-01 08:12:21 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/sqs"
|
2018-10-31 08:11:19 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/notification"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2018-10-31 08:11:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
notification.MessageQueues = append(notification.MessageQueues, &AwsSqsPub{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type AwsSqsPub struct {
|
|
|
|
svc *sqs.SQS
|
|
|
|
queueUrl string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *AwsSqsPub) GetName() string {
|
|
|
|
return "aws_sqs"
|
|
|
|
}
|
|
|
|
|
2020-01-29 17:09:55 +00:00
|
|
|
func (k *AwsSqsPub) Initialize(configuration util.Configuration, prefix string) (err error) {
|
|
|
|
glog.V(0).Infof("filer.notification.aws_sqs.region: %v", configuration.GetString(prefix+"region"))
|
|
|
|
glog.V(0).Infof("filer.notification.aws_sqs.sqs_queue_name: %v", configuration.GetString(prefix+"sqs_queue_name"))
|
2018-10-31 08:11:19 +00:00
|
|
|
return k.initialize(
|
2020-01-29 17:09:55 +00:00
|
|
|
configuration.GetString(prefix+"aws_access_key_id"),
|
|
|
|
configuration.GetString(prefix+"aws_secret_access_key"),
|
|
|
|
configuration.GetString(prefix+"region"),
|
|
|
|
configuration.GetString(prefix+"sqs_queue_name"),
|
2018-10-31 08:11:19 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-11-19 03:24:34 +00:00
|
|
|
func (k *AwsSqsPub) initialize(awsAccessKeyId, awsSecretAccessKey, region, queueName string) (err error) {
|
2018-10-31 08:11:19 +00:00
|
|
|
|
|
|
|
config := &aws.Config{
|
|
|
|
Region: aws.String(region),
|
|
|
|
}
|
2019-11-19 03:24:34 +00:00
|
|
|
if awsAccessKeyId != "" && awsSecretAccessKey != "" {
|
|
|
|
config.Credentials = credentials.NewStaticCredentials(awsAccessKeyId, awsSecretAccessKey, "")
|
2018-10-31 08:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sess, err := session.NewSession(config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create aws session: %v", err)
|
|
|
|
}
|
|
|
|
k.svc = sqs.New(sess)
|
|
|
|
|
|
|
|
result, err := k.svc.GetQueueUrl(&sqs.GetQueueUrlInput{
|
|
|
|
QueueName: aws.String(queueName),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == sqs.ErrCodeQueueDoesNotExist {
|
|
|
|
return fmt.Errorf("unable to find queue %s", queueName)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("get queue %s url: %v", queueName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
k.queueUrl = *result.QueueUrl
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *AwsSqsPub) SendMessage(key string, message proto.Message) (err error) {
|
|
|
|
|
|
|
|
text := proto.MarshalTextString(message)
|
|
|
|
|
|
|
|
_, err = k.svc.SendMessage(&sqs.SendMessageInput{
|
|
|
|
DelaySeconds: aws.Int64(10),
|
|
|
|
MessageAttributes: map[string]*sqs.MessageAttributeValue{
|
|
|
|
"key": &sqs.MessageAttributeValue{
|
|
|
|
DataType: aws.String("String"),
|
|
|
|
StringValue: aws.String(key),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MessageBody: aws.String(text),
|
|
|
|
QueueUrl: &k.queueUrl,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("send message to sqs %s: %v", k.queueUrl, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|