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.

144 lines
3.5 KiB

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