aws_sqs_pub.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/notification"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "github.com/golang/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) (err error) {
  25. glog.V(0).Infof("filer.notification.aws_sqs.region: %v", configuration.GetString("region"))
  26. glog.V(0).Infof("filer.notification.aws_sqs.sqs_queue_name: %v", configuration.GetString("sqs_queue_name"))
  27. return k.initialize(
  28. configuration.GetString("aws_access_key_id"),
  29. configuration.GetString("aws_secret_access_key"),
  30. configuration.GetString("region"),
  31. configuration.GetString("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 := proto.MarshalTextString(message)
  60. _, err = k.svc.SendMessage(&sqs.SendMessageInput{
  61. DelaySeconds: aws.Int64(10),
  62. MessageAttributes: map[string]*sqs.MessageAttributeValue{
  63. "key": &sqs.MessageAttributeValue{
  64. DataType: aws.String("String"),
  65. StringValue: aws.String(key),
  66. },
  67. },
  68. MessageBody: aws.String(text),
  69. QueueUrl: &k.queueUrl,
  70. })
  71. if err != nil {
  72. return fmt.Errorf("send message to sqs %s: %v", k.queueUrl, err)
  73. }
  74. return nil
  75. }