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.

177 lines
4.8 KiB

6 years ago
6 years ago
3 years ago
5 years ago
6 years ago
2 years ago
2 years ago
6 years ago
6 years ago
6 years ago
  1. package S3Sink
  2. import (
  3. "fmt"
  4. "github.com/aws/aws-sdk-go/aws"
  5. "github.com/aws/aws-sdk-go/aws/credentials"
  6. "github.com/aws/aws-sdk-go/aws/session"
  7. "github.com/aws/aws-sdk-go/service/s3"
  8. "github.com/aws/aws-sdk-go/service/s3/s3iface"
  9. "github.com/aws/aws-sdk-go/service/s3/s3manager"
  10. "strings"
  11. "github.com/seaweedfs/seaweedfs/weed/filer"
  12. "github.com/seaweedfs/seaweedfs/weed/glog"
  13. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  14. "github.com/seaweedfs/seaweedfs/weed/replication/sink"
  15. "github.com/seaweedfs/seaweedfs/weed/replication/source"
  16. "github.com/seaweedfs/seaweedfs/weed/util"
  17. )
  18. type S3Sink struct {
  19. conn s3iface.S3API
  20. region string
  21. bucket string
  22. dir string
  23. endpoint string
  24. acl 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.acl: %v", configuration.GetString(prefix+"acl"))
  46. glog.V(0).Infof("sink.s3.is_incremental: %v", configuration.GetString(prefix+"is_incremental"))
  47. s3sink.isIncremental = configuration.GetBool(prefix + "is_incremental")
  48. return s3sink.initialize(
  49. configuration.GetString(prefix+"aws_access_key_id"),
  50. configuration.GetString(prefix+"aws_secret_access_key"),
  51. configuration.GetString(prefix+"region"),
  52. configuration.GetString(prefix+"bucket"),
  53. configuration.GetString(prefix+"directory"),
  54. configuration.GetString(prefix+"endpoint"),
  55. configuration.GetString(prefix+"acl"),
  56. )
  57. }
  58. func (s3sink *S3Sink) SetSourceFiler(s *source.FilerSource) {
  59. s3sink.filerSource = s
  60. }
  61. func (s3sink *S3Sink) initialize(awsAccessKeyId, awsSecretAccessKey, region, bucket, dir, endpoint, acl string) error {
  62. s3sink.region = region
  63. s3sink.bucket = bucket
  64. s3sink.dir = dir
  65. s3sink.endpoint = endpoint
  66. s3sink.acl = acl
  67. config := &aws.Config{
  68. Region: aws.String(s3sink.region),
  69. Endpoint: aws.String(s3sink.endpoint),
  70. S3ForcePathStyle: aws.Bool(true),
  71. S3DisableContentMD5Validation: aws.Bool(true),
  72. }
  73. if awsAccessKeyId != "" && awsSecretAccessKey != "" {
  74. config.Credentials = credentials.NewStaticCredentials(awsAccessKeyId, awsSecretAccessKey, "")
  75. }
  76. sess, err := session.NewSession(config)
  77. if err != nil {
  78. return fmt.Errorf("create aws session: %v", err)
  79. }
  80. s3sink.conn = s3.New(sess)
  81. return nil
  82. }
  83. func (s3sink *S3Sink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) error {
  84. key = cleanKey(key)
  85. if isDirectory {
  86. return nil
  87. }
  88. input := &s3.DeleteObjectInput{
  89. Bucket: aws.String(s3sink.bucket),
  90. Key: aws.String(key),
  91. }
  92. result, err := s3sink.conn.DeleteObject(input)
  93. if err == nil {
  94. glog.V(2).Infof("[%s] delete %s: %v", s3sink.bucket, key, result)
  95. } else {
  96. glog.Errorf("[%s] delete %s: %v", s3sink.bucket, key, err)
  97. }
  98. return err
  99. }
  100. func (s3sink *S3Sink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) (err error) {
  101. key = cleanKey(key)
  102. if entry.IsDirectory {
  103. return nil
  104. }
  105. reader := filer.NewFileReader(s3sink.filerSource, entry)
  106. fileSize := int64(filer.FileSize(entry))
  107. partSize := int64(8 * 1024 * 1024) // The minimum/default allowed part size is 5MB
  108. for partSize*1000 < fileSize {
  109. partSize *= 4
  110. }
  111. // Create an uploader with the session and custom options
  112. uploader := s3manager.NewUploaderWithClient(s3sink.conn, func(u *s3manager.Uploader) {
  113. u.PartSize = partSize
  114. u.Concurrency = 8
  115. })
  116. // process tagging
  117. tags := ""
  118. if true {
  119. for k, v := range entry.Extended {
  120. if len(tags) > 0 {
  121. tags = tags + "&"
  122. }
  123. tags = tags + k + "=" + string(v)
  124. }
  125. }
  126. // Upload the file to S3.
  127. _, err = uploader.Upload(&s3manager.UploadInput{
  128. Bucket: aws.String(s3sink.bucket),
  129. Key: aws.String(key),
  130. Body: reader,
  131. Tagging: aws.String(tags),
  132. })
  133. return
  134. }
  135. func (s3sink *S3Sink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool, signatures []int32) (foundExistingEntry bool, err error) {
  136. key = cleanKey(key)
  137. return true, s3sink.CreateEntry(key, newEntry, signatures)
  138. }
  139. func cleanKey(key string) string {
  140. if strings.HasPrefix(key, "/") {
  141. key = key[1:]
  142. }
  143. return key
  144. }