s3_sink.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package S3Sink
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "sync"
  7. "github.com/aws/aws-sdk-go/aws"
  8. "github.com/aws/aws-sdk-go/aws/credentials"
  9. "github.com/aws/aws-sdk-go/aws/session"
  10. "github.com/aws/aws-sdk-go/service/s3"
  11. "github.com/aws/aws-sdk-go/service/s3/s3iface"
  12. "github.com/chrislusf/seaweedfs/weed/filer"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  15. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  16. "github.com/chrislusf/seaweedfs/weed/replication/source"
  17. "github.com/chrislusf/seaweedfs/weed/util"
  18. )
  19. type S3Sink struct {
  20. conn s3iface.S3API
  21. region string
  22. bucket string
  23. dir string
  24. endpoint string
  25. filerSource *source.FilerSource
  26. isIncremental bool
  27. }
  28. func init() {
  29. sink.Sinks = append(sink.Sinks, &S3Sink{})
  30. }
  31. func (s3sink *S3Sink) GetName() string {
  32. return "s3"
  33. }
  34. func (s3sink *S3Sink) GetSinkToDirectory() string {
  35. return s3sink.dir
  36. }
  37. func (s3sink *S3Sink) IsIncremental() bool {
  38. return s3sink.isIncremental
  39. }
  40. func (s3sink *S3Sink) Initialize(configuration util.Configuration, prefix string) error {
  41. glog.V(0).Infof("sink.s3.region: %v", configuration.GetString(prefix+"region"))
  42. glog.V(0).Infof("sink.s3.bucket: %v", configuration.GetString(prefix+"bucket"))
  43. glog.V(0).Infof("sink.s3.directory: %v", configuration.GetString(prefix+"directory"))
  44. glog.V(0).Infof("sink.s3.endpoint: %v", configuration.GetString(prefix+"endpoint"))
  45. glog.V(0).Infof("sink.s3.is_incremental: %v", configuration.GetString(prefix+"is_incremental"))
  46. s3sink.isIncremental = configuration.GetBool(prefix + "is_incremental")
  47. return s3sink.initialize(
  48. configuration.GetString(prefix+"aws_access_key_id"),
  49. configuration.GetString(prefix+"aws_secret_access_key"),
  50. configuration.GetString(prefix+"region"),
  51. configuration.GetString(prefix+"bucket"),
  52. configuration.GetString(prefix+"directory"),
  53. configuration.GetString(prefix+"endpoint"),
  54. )
  55. }
  56. func (s3sink *S3Sink) SetSourceFiler(s *source.FilerSource) {
  57. s3sink.filerSource = s
  58. }
  59. func (s3sink *S3Sink) initialize(awsAccessKeyId, awsSecretAccessKey, region, bucket, dir, endpoint string) error {
  60. s3sink.region = region
  61. s3sink.bucket = bucket
  62. s3sink.dir = dir
  63. s3sink.endpoint = endpoint
  64. config := &aws.Config{
  65. Region: aws.String(s3sink.region),
  66. Endpoint: aws.String(s3sink.endpoint),
  67. S3ForcePathStyle: aws.Bool(true),
  68. S3DisableContentMD5Validation: aws.Bool(true),
  69. }
  70. if awsAccessKeyId != "" && awsSecretAccessKey != "" {
  71. config.Credentials = credentials.NewStaticCredentials(awsAccessKeyId, awsSecretAccessKey, "")
  72. }
  73. sess, err := session.NewSession(config)
  74. if err != nil {
  75. return fmt.Errorf("create aws session: %v", err)
  76. }
  77. s3sink.conn = s3.New(sess)
  78. return nil
  79. }
  80. func (s3sink *S3Sink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) error {
  81. key = cleanKey(key)
  82. if isDirectory {
  83. key = key + "/"
  84. }
  85. return s3sink.deleteObject(key)
  86. }
  87. func (s3sink *S3Sink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) error {
  88. key = cleanKey(key)
  89. if entry.IsDirectory {
  90. return nil
  91. }
  92. uploadId, err := s3sink.createMultipartUpload(key, entry)
  93. if err != nil {
  94. return fmt.Errorf("createMultipartUpload: %v", err)
  95. }
  96. totalSize := filer.FileSize(entry)
  97. chunkViews := filer.ViewFromChunks(s3sink.filerSource.LookupFileId, entry.Chunks, 0, int64(totalSize))
  98. parts := make([]*s3.CompletedPart, len(chunkViews))
  99. var wg sync.WaitGroup
  100. for chunkIndex, chunk := range chunkViews {
  101. partId := chunkIndex + 1
  102. wg.Add(1)
  103. go func(chunk *filer.ChunkView, index int) {
  104. defer wg.Done()
  105. if part, uploadErr := s3sink.uploadPart(key, uploadId, partId, chunk); uploadErr != nil {
  106. err = uploadErr
  107. glog.Errorf("uploadPart: %v", uploadErr)
  108. } else {
  109. parts[index] = part
  110. }
  111. }(chunk, chunkIndex)
  112. }
  113. wg.Wait()
  114. if err != nil {
  115. s3sink.abortMultipartUpload(key, uploadId)
  116. return fmt.Errorf("uploadPart: %v", err)
  117. }
  118. return s3sink.completeMultipartUpload(context.Background(), key, uploadId, parts)
  119. }
  120. func (s3sink *S3Sink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool, signatures []int32) (foundExistingEntry bool, err error) {
  121. key = cleanKey(key)
  122. return true, s3sink.CreateEntry(key, newEntry, signatures)
  123. }
  124. func cleanKey(key string) string {
  125. if strings.HasPrefix(key, "/") {
  126. key = key[1:]
  127. }
  128. return key
  129. }