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.

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