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.

128 lines
2.5 KiB

  1. package B2Sink
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/filer2"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  7. "github.com/chrislusf/seaweedfs/weed/replication/source"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "github.com/kurin/blazer/b2"
  10. )
  11. type B2Sink struct {
  12. client *b2.Client
  13. bucket string
  14. dir string
  15. filerSource *source.FilerSource
  16. }
  17. func init() {
  18. sink.Sinks = append(sink.Sinks, &B2Sink{})
  19. }
  20. func (g *B2Sink) GetName() string {
  21. return "backblaze"
  22. }
  23. func (g *B2Sink) GetSinkToDirectory() string {
  24. return g.dir
  25. }
  26. func (g *B2Sink) Initialize(configuration util.Configuration) error {
  27. return g.initialize(
  28. configuration.GetString("account_id"),
  29. configuration.GetString("account_key"),
  30. configuration.GetString("bucket"),
  31. configuration.GetString("directory"),
  32. )
  33. }
  34. func (g *B2Sink) SetSourceFiler(s *source.FilerSource) {
  35. g.filerSource = s
  36. }
  37. func (g *B2Sink) initialize(accountId, accountKey, bucket, dir string) error {
  38. ctx := context.Background()
  39. client, err := b2.NewClient(ctx, accountId, accountKey)
  40. if err != nil {
  41. return nil
  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. if isDirectory {
  50. key = key + "/"
  51. }
  52. ctx := context.Background()
  53. bucket, err := g.client.Bucket(ctx, g.bucket)
  54. if err != nil {
  55. return err
  56. }
  57. targetObject := bucket.Object(key)
  58. return targetObject.Delete(ctx)
  59. }
  60. func (g *B2Sink) CreateEntry(key string, entry *filer_pb.Entry) error {
  61. if entry.IsDirectory {
  62. return nil
  63. }
  64. totalSize := filer2.TotalSize(entry.Chunks)
  65. chunkViews := filer2.ViewFromChunks(entry.Chunks, 0, int(totalSize))
  66. ctx := context.Background()
  67. bucket, err := g.client.Bucket(ctx, g.bucket)
  68. if err != nil {
  69. return err
  70. }
  71. targetObject := bucket.Object(key)
  72. writer := targetObject.NewWriter(ctx)
  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, 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, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (foundExistingEntry bool, err error) {
  95. // TODO improve efficiency
  96. return false, nil
  97. }