seaweedfs/weed/replication/sink/b2sink/b2_sink.go

134 lines
3.0 KiB
Go
Raw Normal View History

2018-10-24 14:59:40 +08:00
package B2Sink
import (
"context"
"github.com/seaweedfs/seaweedfs/weed/replication/repl_util"
2018-11-05 03:58:59 +08:00
"strings"
2018-10-24 14:59:40 +08:00
"github.com/kurin/blazer/b2"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/replication/sink"
"github.com/seaweedfs/seaweedfs/weed/replication/source"
"github.com/seaweedfs/seaweedfs/weed/util"
2018-10-24 14:59:40 +08:00
)
type B2Sink struct {
2021-03-01 12:34:14 +08:00
client *b2.Client
bucket string
dir string
filerSource *source.FilerSource
2021-03-01 08:19:03 +08:00
isIncremental bool
2018-10-24 14:59:40 +08:00
}
func init() {
sink.Sinks = append(sink.Sinks, &B2Sink{})
}
func (g *B2Sink) GetName() string {
return "backblaze"
}
func (g *B2Sink) GetSinkToDirectory() string {
return g.dir
}
2021-03-01 08:19:03 +08:00
func (g *B2Sink) IsIncremental() bool {
return g.isIncremental
}
func (g *B2Sink) Initialize(configuration util.Configuration, prefix string) error {
2021-03-01 12:34:14 +08:00
g.isIncremental = configuration.GetBool(prefix + "is_incremental")
2018-10-24 14:59:40 +08:00
return g.initialize(
configuration.GetString(prefix+"b2_account_id"),
configuration.GetString(prefix+"b2_master_application_key"),
configuration.GetString(prefix+"bucket"),
configuration.GetString(prefix+"directory"),
2018-10-24 14:59:40 +08:00
)
}
func (g *B2Sink) SetSourceFiler(s *source.FilerSource) {
g.filerSource = s
}
func (g *B2Sink) initialize(accountId, accountKey, bucket, dir string) error {
2020-02-26 14:23:59 +08:00
client, err := b2.NewClient(context.Background(), accountId, accountKey)
2018-10-24 14:59:40 +08:00
if err != nil {
2018-11-05 03:58:59 +08:00
return err
2018-10-24 14:59:40 +08:00
}
g.client = client
g.bucket = bucket
g.dir = dir
return nil
}
2020-09-10 02:21:23 +08:00
func (g *B2Sink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) error {
2018-10-24 14:59:40 +08:00
2018-11-05 03:58:59 +08:00
key = cleanKey(key)
2018-10-24 14:59:40 +08:00
if isDirectory {
key = key + "/"
}
bucket, err := g.client.Bucket(context.Background(), g.bucket)
2018-10-24 14:59:40 +08:00
if err != nil {
return err
}
targetObject := bucket.Object(key)
return targetObject.Delete(context.Background())
2018-10-24 14:59:40 +08:00
}
2020-09-10 02:21:23 +08:00
func (g *B2Sink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) error {
2018-10-24 14:59:40 +08:00
2018-11-05 03:58:59 +08:00
key = cleanKey(key)
2018-10-24 14:59:40 +08:00
if entry.IsDirectory {
return nil
}
2020-09-01 15:21:19 +08:00
totalSize := filer.FileSize(entry)
chunkViews := filer.ViewFromChunks(g.filerSource.LookupFileId, entry.GetChunks(), 0, int64(totalSize))
2018-10-24 14:59:40 +08:00
bucket, err := g.client.Bucket(context.Background(), g.bucket)
2018-10-24 14:59:40 +08:00
if err != nil {
return err
}
targetObject := bucket.Object(key)
writer := targetObject.NewWriter(context.Background())
defer writer.Close()
2018-10-24 14:59:40 +08:00
writeFunc := func(data []byte) error {
_, writeErr := writer.Write(data)
return writeErr
}
2018-10-24 14:59:40 +08:00
if len(entry.Content) > 0 {
return writeFunc(entry.Content)
}
2018-10-24 14:59:40 +08:00
if err := repl_util.CopyFromChunkViews(chunkViews, g.filerSource, writeFunc); err != nil {
return err
2018-10-24 14:59:40 +08:00
}
return nil
2018-10-24 14:59:40 +08:00
}
2020-09-10 02:21:23 +08:00
func (g *B2Sink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool, signatures []int32) (foundExistingEntry bool, err error) {
2018-11-05 03:58:59 +08:00
key = cleanKey(key)
return true, g.CreateEntry(key, newEntry, signatures)
2018-10-24 14:59:40 +08:00
}
2018-11-05 03:58:59 +08:00
func cleanKey(key string) string {
if strings.HasPrefix(key, "/") {
key = key[1:]
}
return key
}