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.

150 lines
3.9 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. }
  27. func init() {
  28. sink.Sinks = append(sink.Sinks, &S3Sink{})
  29. }
  30. func (s3sink *S3Sink) GetName() string {
  31. return "s3"
  32. }
  33. func (s3sink *S3Sink) GetSinkToDirectory() string {
  34. return s3sink.dir
  35. }
  36. func (s3sink *S3Sink) Initialize(configuration util.Configuration, prefix string) error {
  37. glog.V(0).Infof("sink.s3.region: %v", configuration.GetString(prefix+"region"))
  38. glog.V(0).Infof("sink.s3.bucket: %v", configuration.GetString(prefix+"bucket"))
  39. glog.V(0).Infof("sink.s3.directory: %v", configuration.GetString(prefix+"directory"))
  40. glog.V(0).Infof("sink.s3.endpoint: %v", configuration.GetString(prefix+"endpoint"))
  41. return s3sink.initialize(
  42. configuration.GetString(prefix+"aws_access_key_id"),
  43. configuration.GetString(prefix+"aws_secret_access_key"),
  44. configuration.GetString(prefix+"region"),
  45. configuration.GetString(prefix+"bucket"),
  46. configuration.GetString(prefix+"directory"),
  47. configuration.GetString(prefix+"endpoint"),
  48. )
  49. }
  50. func (s3sink *S3Sink) SetSourceFiler(s *source.FilerSource) {
  51. s3sink.filerSource = s
  52. }
  53. func (s3sink *S3Sink) initialize(awsAccessKeyId, awsSecretAccessKey, region, bucket, dir, endpoint string) error {
  54. s3sink.region = region
  55. s3sink.bucket = bucket
  56. s3sink.dir = dir
  57. s3sink.endpoint = endpoint
  58. config := &aws.Config{
  59. Region: aws.String(s3sink.region),
  60. Endpoint: aws.String(s3sink.endpoint),
  61. }
  62. if awsAccessKeyId != "" && awsSecretAccessKey != "" {
  63. config.Credentials = credentials.NewStaticCredentials(awsAccessKeyId, awsSecretAccessKey, "")
  64. }
  65. sess, err := session.NewSession(config)
  66. if err != nil {
  67. return fmt.Errorf("create aws session: %v", err)
  68. }
  69. s3sink.conn = s3.New(sess)
  70. return nil
  71. }
  72. func (s3sink *S3Sink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) error {
  73. key = cleanKey(key)
  74. if isDirectory {
  75. key = key + "/"
  76. }
  77. return s3sink.deleteObject(key)
  78. }
  79. func (s3sink *S3Sink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) error {
  80. key = cleanKey(key)
  81. if entry.IsDirectory {
  82. return nil
  83. }
  84. uploadId, err := s3sink.createMultipartUpload(key, entry)
  85. if err != nil {
  86. return err
  87. }
  88. totalSize := filer.FileSize(entry)
  89. chunkViews := filer.ViewFromChunks(s3sink.filerSource.LookupFileId, entry.Chunks, 0, int64(totalSize))
  90. parts := make([]*s3.CompletedPart, len(chunkViews))
  91. var wg sync.WaitGroup
  92. for chunkIndex, chunk := range chunkViews {
  93. partId := chunkIndex + 1
  94. wg.Add(1)
  95. go func(chunk *filer.ChunkView, index int) {
  96. defer wg.Done()
  97. if part, uploadErr := s3sink.uploadPart(key, uploadId, partId, chunk); uploadErr != nil {
  98. err = uploadErr
  99. } else {
  100. parts[index] = part
  101. }
  102. }(chunk, chunkIndex)
  103. }
  104. wg.Wait()
  105. if err != nil {
  106. s3sink.abortMultipartUpload(key, uploadId)
  107. return err
  108. }
  109. return s3sink.completeMultipartUpload(context.Background(), key, uploadId, parts)
  110. }
  111. func (s3sink *S3Sink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool, signatures []int32) (foundExistingEntry bool, err error) {
  112. key = cleanKey(key)
  113. // TODO improve efficiency
  114. return false, nil
  115. }
  116. func cleanKey(key string) string {
  117. if strings.HasPrefix(key, "/") {
  118. key = key[1:]
  119. }
  120. return key
  121. }