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.

158 lines
4.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
  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. }
  69. if awsAccessKeyId != "" && awsSecretAccessKey != "" {
  70. config.Credentials = credentials.NewStaticCredentials(awsAccessKeyId, awsSecretAccessKey, "")
  71. }
  72. sess, err := session.NewSession(config)
  73. if err != nil {
  74. return fmt.Errorf("create aws session: %v", err)
  75. }
  76. s3sink.conn = s3.New(sess)
  77. return nil
  78. }
  79. func (s3sink *S3Sink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) error {
  80. key = cleanKey(key)
  81. if isDirectory {
  82. key = key + "/"
  83. }
  84. return s3sink.deleteObject(key)
  85. }
  86. func (s3sink *S3Sink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) error {
  87. key = cleanKey(key)
  88. if entry.IsDirectory {
  89. return nil
  90. }
  91. uploadId, err := s3sink.createMultipartUpload(key, entry)
  92. if err != nil {
  93. return fmt.Errorf("createMultipartUpload: %v", err)
  94. }
  95. totalSize := filer.FileSize(entry)
  96. chunkViews := filer.ViewFromChunks(s3sink.filerSource.LookupFileId, entry.Chunks, 0, int64(totalSize))
  97. parts := make([]*s3.CompletedPart, len(chunkViews))
  98. var wg sync.WaitGroup
  99. for chunkIndex, chunk := range chunkViews {
  100. partId := chunkIndex + 1
  101. wg.Add(1)
  102. go func(chunk *filer.ChunkView, index int) {
  103. defer wg.Done()
  104. if part, uploadErr := s3sink.uploadPart(key, uploadId, partId, chunk); uploadErr != nil {
  105. err = uploadErr
  106. glog.Errorf("uploadPart: %v", uploadErr)
  107. } else {
  108. parts[index] = part
  109. }
  110. }(chunk, chunkIndex)
  111. }
  112. wg.Wait()
  113. if err != nil {
  114. s3sink.abortMultipartUpload(key, uploadId)
  115. return fmt.Errorf("uploadPart: %v", err)
  116. }
  117. return s3sink.completeMultipartUpload(context.Background(), key, uploadId, parts)
  118. }
  119. func (s3sink *S3Sink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool, signatures []int32) (foundExistingEntry bool, err error) {
  120. key = cleanKey(key)
  121. return true, s3sink.CreateEntry(key, newEntry, signatures)
  122. }
  123. func cleanKey(key string) string {
  124. if strings.HasPrefix(key, "/") {
  125. key = key[1:]
  126. }
  127. return key
  128. }