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.

139 lines
2.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
  1. package B2Sink
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/chrislusf/seaweedfs/weed/filer2"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  8. "github.com/chrislusf/seaweedfs/weed/replication/source"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. "github.com/kurin/blazer/b2"
  11. )
  12. type B2Sink struct {
  13. client *b2.Client
  14. bucket string
  15. dir string
  16. filerSource *source.FilerSource
  17. }
  18. func init() {
  19. sink.Sinks = append(sink.Sinks, &B2Sink{})
  20. }
  21. func (g *B2Sink) GetName() string {
  22. return "backblaze"
  23. }
  24. func (g *B2Sink) GetSinkToDirectory() string {
  25. return g.dir
  26. }
  27. func (g *B2Sink) Initialize(configuration util.Configuration, prefix string) error {
  28. return g.initialize(
  29. configuration.GetString(prefix+"b2_account_id"),
  30. configuration.GetString(prefix+"b2_master_application_key"),
  31. configuration.GetString(prefix+"bucket"),
  32. configuration.GetString(prefix+"directory"),
  33. )
  34. }
  35. func (g *B2Sink) SetSourceFiler(s *source.FilerSource) {
  36. g.filerSource = s
  37. }
  38. func (g *B2Sink) initialize(accountId, accountKey, bucket, dir string) error {
  39. client, err := b2.NewClient(context.Background(), accountId, accountKey)
  40. if err != nil {
  41. return err
  42. }
  43. g.client = client
  44. g.bucket = bucket
  45. g.dir = dir
  46. return nil
  47. }
  48. func (g *B2Sink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool) error {
  49. key = cleanKey(key)
  50. if isDirectory {
  51. key = key + "/"
  52. }
  53. bucket, err := g.client.Bucket(context.Background(), g.bucket)
  54. if err != nil {
  55. return err
  56. }
  57. targetObject := bucket.Object(key)
  58. return targetObject.Delete(context.Background())
  59. }
  60. func (g *B2Sink) CreateEntry(key string, entry *filer_pb.Entry) error {
  61. key = cleanKey(key)
  62. if entry.IsDirectory {
  63. return nil
  64. }
  65. totalSize := filer2.TotalSize(entry.Chunks)
  66. chunkViews := filer2.ViewFromChunks(g.filerSource.LookupFileId, entry.Chunks, 0, int64(totalSize))
  67. bucket, err := g.client.Bucket(context.Background(), g.bucket)
  68. if err != nil {
  69. return err
  70. }
  71. targetObject := bucket.Object(key)
  72. writer := targetObject.NewWriter(context.Background())
  73. for _, chunk := range chunkViews {
  74. fileUrl, err := g.filerSource.LookupFileId(chunk.FileId)
  75. if err != nil {
  76. return err
  77. }
  78. var writeErr error
  79. readErr := util.ReadUrlAsStream(fileUrl, nil, false, chunk.IsFullChunk(), chunk.Offset, int(chunk.Size), func(data []byte) {
  80. _, err := writer.Write(data)
  81. if err != nil {
  82. writeErr = err
  83. }
  84. })
  85. if readErr != nil {
  86. return readErr
  87. }
  88. if writeErr != nil {
  89. return writeErr
  90. }
  91. }
  92. return writer.Close()
  93. }
  94. func (g *B2Sink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (foundExistingEntry bool, err error) {
  95. key = cleanKey(key)
  96. // TODO improve efficiency
  97. return false, nil
  98. }
  99. func cleanKey(key string) string {
  100. if strings.HasPrefix(key, "/") {
  101. key = key[1:]
  102. }
  103. return key
  104. }