You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.9 KiB

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