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.

126 lines
3.0 KiB

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