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.

185 lines
5.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package s3_backend
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "strings"
  7. "time"
  8. "github.com/aws/aws-sdk-go/service/s3"
  9. "github.com/aws/aws-sdk-go/service/s3/s3iface"
  10. "github.com/google/uuid"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  13. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  14. )
  15. func init() {
  16. backend.BackendStorageFactories["s3"] = &S3BackendFactory{}
  17. }
  18. type S3BackendFactory struct {
  19. }
  20. func (factory *S3BackendFactory) StorageType() backend.StorageType {
  21. return backend.StorageType("s3")
  22. }
  23. func (factory *S3BackendFactory) BuildStorage(configuration backend.StringProperties, configPrefix string, id string) (backend.BackendStorage, error) {
  24. return newS3BackendStorage(configuration, configPrefix, id)
  25. }
  26. type S3BackendStorage struct {
  27. id string
  28. aws_access_key_id string
  29. aws_secret_access_key string
  30. region string
  31. bucket string
  32. endpoint string
  33. conn s3iface.S3API
  34. }
  35. func newS3BackendStorage(configuration backend.StringProperties, configPrefix string, id string) (s *S3BackendStorage, err error) {
  36. s = &S3BackendStorage{}
  37. s.id = id
  38. s.aws_access_key_id = configuration.GetString(configPrefix + "aws_access_key_id")
  39. s.aws_secret_access_key = configuration.GetString(configPrefix + "aws_secret_access_key")
  40. s.region = configuration.GetString(configPrefix + "region")
  41. s.bucket = configuration.GetString(configPrefix + "bucket")
  42. s.endpoint = configuration.GetString(configPrefix + "endpoint")
  43. s.conn, err = createSession(s.aws_access_key_id, s.aws_secret_access_key, s.region, s.endpoint)
  44. glog.V(0).Infof("created backend storage s3.%s for region %s bucket %s", s.id, s.region, s.bucket)
  45. return
  46. }
  47. func (s *S3BackendStorage) ToProperties() map[string]string {
  48. m := make(map[string]string)
  49. m["aws_access_key_id"] = s.aws_access_key_id
  50. m["aws_secret_access_key"] = s.aws_secret_access_key
  51. m["region"] = s.region
  52. m["bucket"] = s.bucket
  53. m["endpoint"] = s.endpoint
  54. return m
  55. }
  56. func (s *S3BackendStorage) NewStorageFile(key string, tierInfo *volume_server_pb.VolumeInfo) backend.BackendStorageFile {
  57. if strings.HasPrefix(key, "/") {
  58. key = key[1:]
  59. }
  60. f := &S3BackendStorageFile{
  61. backendStorage: s,
  62. key: key,
  63. tierInfo: tierInfo,
  64. }
  65. return f
  66. }
  67. func (s *S3BackendStorage) CopyFile(f *os.File, fn func(progressed int64, percentage float32) error) (key string, size int64, err error) {
  68. randomUuid, _ := uuid.NewRandom()
  69. key = randomUuid.String()
  70. glog.V(1).Infof("copying dat file of %s to remote s3.%s as %s", f.Name(), s.id, key)
  71. size, err = uploadToS3(s.conn, f.Name(), s.bucket, key, fn)
  72. return
  73. }
  74. func (s *S3BackendStorage) DownloadFile(fileName string, key string, fn func(progressed int64, percentage float32) error) (size int64, err error) {
  75. glog.V(1).Infof("download dat file of %s from remote s3.%s as %s", fileName, s.id, key)
  76. size, err = downloadFromS3(s.conn, fileName, s.bucket, key, fn)
  77. return
  78. }
  79. func (s *S3BackendStorage) DeleteFile(key string) (err error) {
  80. glog.V(1).Infof("delete dat file %s from remote", key)
  81. err = deleteFromS3(s.conn, s.bucket, key)
  82. return
  83. }
  84. type S3BackendStorageFile struct {
  85. backendStorage *S3BackendStorage
  86. key string
  87. tierInfo *volume_server_pb.VolumeInfo
  88. }
  89. func (s3backendStorageFile S3BackendStorageFile) ReadAt(p []byte, off int64) (n int, err error) {
  90. bytesRange := fmt.Sprintf("bytes=%d-%d", off, off+int64(len(p))-1)
  91. // glog.V(0).Infof("read %s %s", s3backendStorageFile.key, bytesRange)
  92. getObjectOutput, getObjectErr := s3backendStorageFile.backendStorage.conn.GetObject(&s3.GetObjectInput{
  93. Bucket: &s3backendStorageFile.backendStorage.bucket,
  94. Key: &s3backendStorageFile.key,
  95. Range: &bytesRange,
  96. })
  97. if getObjectErr != nil {
  98. return 0, fmt.Errorf("bucket %s GetObject %s: %v", s3backendStorageFile.backendStorage.bucket, s3backendStorageFile.key, getObjectErr)
  99. }
  100. defer getObjectOutput.Body.Close()
  101. glog.V(4).Infof("read %s %s", s3backendStorageFile.key, bytesRange)
  102. glog.V(4).Infof("content range: %s, contentLength: %d", *getObjectOutput.ContentRange, *getObjectOutput.ContentLength)
  103. for {
  104. if n, err = getObjectOutput.Body.Read(p); err == nil && n < len(p) {
  105. p = p[n:]
  106. } else {
  107. break
  108. }
  109. }
  110. if err == io.EOF {
  111. err = nil
  112. }
  113. return
  114. }
  115. func (s3backendStorageFile S3BackendStorageFile) WriteAt(p []byte, off int64) (n int, err error) {
  116. panic("not implemented")
  117. }
  118. func (s3backendStorageFile S3BackendStorageFile) Truncate(off int64) error {
  119. panic("not implemented")
  120. }
  121. func (s3backendStorageFile S3BackendStorageFile) Close() error {
  122. return nil
  123. }
  124. func (s3backendStorageFile S3BackendStorageFile) GetStat() (datSize int64, modTime time.Time, err error) {
  125. files := s3backendStorageFile.tierInfo.GetFiles()
  126. if len(files) == 0 {
  127. err = fmt.Errorf("remote file info not found")
  128. return
  129. }
  130. datSize = int64(files[0].FileSize)
  131. modTime = time.Unix(int64(files[0].ModifiedTime), 0)
  132. return
  133. }
  134. func (s3backendStorageFile S3BackendStorageFile) Name() string {
  135. return s3backendStorageFile.key
  136. }
  137. func (s3backendStorageFile S3BackendStorageFile) Sync() error {
  138. return nil
  139. }