aws_sqs_pub.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package aws_sqs
  2. import (
  3. "fmt"
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/aws/awserr"
  6. "github.com/aws/aws-sdk-go/aws/credentials"
  7. "github.com/aws/aws-sdk-go/aws/session"
  8. "github.com/aws/aws-sdk-go/service/sqs"
  9. "github.com/seaweedfs/seaweedfs/weed/glog"
  10. "github.com/seaweedfs/seaweedfs/weed/notification"
  11. "github.com/seaweedfs/seaweedfs/weed/util"
  12. "google.golang.org/protobuf/proto"
  13. )
  14. func init() {
  15. notification.MessageQueues = append(notification.MessageQueues, &AwsSqsPub{})
  16. }
  17. type AwsSqsPub struct {
  18. svc *sqs.SQS
  19. queueUrl string
  20. }
  21. func (k *AwsSqsPub) GetName() string {
  22. return "aws_sqs"
  23. }
  24. func (k *AwsSqsPub) Initialize(configuration util.Configuration, prefix string) (err error) {
  25. glog.V(0).Infof("filer.notification.aws_sqs.region: %v", configuration.GetString(prefix+"region"))
  26. glog.V(0).Infof("filer.notification.aws_sqs.sqs_queue_name: %v", configuration.GetString(prefix+"sqs_queue_name"))
  27. return k.initialize(
  28. configuration.GetString(prefix+"aws_access_key_id"),
  29. configuration.GetString(prefix+"aws_secret_access_key"),
  30. configuration.GetString(prefix+"region"),
  31. configuration.GetString(prefix+"sqs_queue_name"),
  32. )
  33. }
  34. func (k *AwsSqsPub) initialize(awsAccessKeyId, awsSecretAccessKey, region, queueName string) (err error) {
  35. config := &aws.Config{
  36. Region: aws.String(region),
  37. }
  38. if awsAccessKeyId != "" && awsSecretAccessKey != "" {
  39. config.Credentials = credentials.NewStaticCredentials(awsAccessKeyId, awsSecretAccessKey, "")
  40. }
  41. sess, err := session.NewSession(config)
  42. if err != nil {
  43. return fmt.Errorf("create aws session: %v", err)
  44. }
  45. k.svc = sqs.New(sess)
  46. result, err := k.svc.GetQueueUrl(&sqs.GetQueueUrlInput{
  47. QueueName: aws.String(queueName),
  48. })
  49. if err != nil {
  50. if aerr, ok := err.(awserr.Error); ok && aerr.Code() == sqs.ErrCodeQueueDoesNotExist {
  51. return fmt.Errorf("unable to find queue %s", queueName)
  52. }
  53. return fmt.Errorf("get queue %s url: %v", queueName, err)
  54. }
  55. k.queueUrl = *result.QueueUrl
  56. return nil
  57. }
  58. func (k *AwsSqsPub) SendMessage(key string, message proto.Message) (err error) {
  59. text, err := proto.Marshal(message)
  60. if err != nil {
  61. return fmt.Errorf("send message marshal %+v: %v", message, err)
  62. }
  63. _, err = k.svc.SendMessage(&sqs.SendMessageInput{
  64. DelaySeconds: aws.Int64(10),
  65. MessageAttributes: map[string]*sqs.MessageAttributeValue{
  66. "key": &sqs.MessageAttributeValue{
  67. DataType: aws.String("String"),
  68. StringValue: aws.String(key),
  69. },
  70. },
  71. MessageBody: aws.String(string(text)),
  72. QueueUrl: &k.queueUrl,
  73. })
  74. if err != nil {
  75. return fmt.Errorf("send message to sqs %s: %v", k.queueUrl, err)
  76. }
  77. return nil
  78. }