2018-10-24 06:59:40 +00:00
|
|
|
package B2Sink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-04 19:58:59 +00:00
|
|
|
"strings"
|
|
|
|
|
2018-10-24 06:59:40 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/replication/sink"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/replication/source"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
"github.com/kurin/blazer/b2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type B2Sink struct {
|
|
|
|
client *b2.Client
|
|
|
|
bucket string
|
|
|
|
dir string
|
|
|
|
filerSource *source.FilerSource
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
sink.Sinks = append(sink.Sinks, &B2Sink{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *B2Sink) GetName() string {
|
|
|
|
return "backblaze"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *B2Sink) GetSinkToDirectory() string {
|
|
|
|
return g.dir
|
|
|
|
}
|
|
|
|
|
2020-01-29 17:09:55 +00:00
|
|
|
func (g *B2Sink) Initialize(configuration util.Configuration, prefix string) error {
|
2018-10-24 06:59:40 +00:00
|
|
|
return g.initialize(
|
2020-01-29 17:09:55 +00:00
|
|
|
configuration.GetString(prefix+"b2_account_id"),
|
|
|
|
configuration.GetString(prefix+"b2_master_application_key"),
|
|
|
|
configuration.GetString(prefix+"bucket"),
|
|
|
|
configuration.GetString(prefix+"directory"),
|
2018-10-24 06:59:40 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *B2Sink) SetSourceFiler(s *source.FilerSource) {
|
|
|
|
g.filerSource = s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *B2Sink) initialize(accountId, accountKey, bucket, dir string) error {
|
2020-02-26 06:23:59 +00:00
|
|
|
client, err := b2.NewClient(context.Background(), accountId, accountKey)
|
2018-10-24 06:59:40 +00:00
|
|
|
if err != nil {
|
2018-11-04 19:58:59 +00:00
|
|
|
return err
|
2018-10-24 06:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g.client = client
|
|
|
|
g.bucket = bucket
|
|
|
|
g.dir = dir
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
func (g *B2Sink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool) error {
|
2018-10-24 06:59:40 +00:00
|
|
|
|
2018-11-04 19:58:59 +00:00
|
|
|
key = cleanKey(key)
|
|
|
|
|
2018-10-24 06:59:40 +00:00
|
|
|
if isDirectory {
|
|
|
|
key = key + "/"
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
bucket, err := g.client.Bucket(context.Background(), g.bucket)
|
2018-10-24 06:59:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
targetObject := bucket.Object(key)
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
return targetObject.Delete(context.Background())
|
2018-10-24 06:59:40 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
func (g *B2Sink) CreateEntry(key string, entry *filer_pb.Entry) error {
|
2018-10-24 06:59:40 +00:00
|
|
|
|
2018-11-04 19:58:59 +00:00
|
|
|
key = cleanKey(key)
|
|
|
|
|
2018-10-24 06:59:40 +00:00
|
|
|
if entry.IsDirectory {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-15 16:32:47 +00:00
|
|
|
totalSize := filer2.FileSize(entry)
|
2020-07-20 00:59:43 +00:00
|
|
|
chunkViews := filer2.ViewFromChunks(g.filerSource.LookupFileId, entry.Chunks, 0, int64(totalSize))
|
2018-10-24 06:59:40 +00:00
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
bucket, err := g.client.Bucket(context.Background(), g.bucket)
|
2018-10-24 06:59:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
targetObject := bucket.Object(key)
|
2020-02-26 05:50:12 +00:00
|
|
|
writer := targetObject.NewWriter(context.Background())
|
2018-10-24 06:59:40 +00:00
|
|
|
|
|
|
|
for _, chunk := range chunkViews {
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
fileUrl, err := g.filerSource.LookupFileId(chunk.FileId)
|
2018-10-24 06:59:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var writeErr error
|
2020-08-19 07:43:07 +00:00
|
|
|
readErr := util.ReadUrlAsStream(fileUrl+"?readDeleted=true", nil, false, chunk.IsFullChunk(), chunk.Offset, int(chunk.Size), func(data []byte) {
|
2018-10-24 06:59:40 +00:00
|
|
|
_, err := writer.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
writeErr = err
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if readErr != nil {
|
|
|
|
return readErr
|
|
|
|
}
|
|
|
|
if writeErr != nil {
|
|
|
|
return writeErr
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return writer.Close()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
func (g *B2Sink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (foundExistingEntry bool, err error) {
|
2018-11-04 19:58:59 +00:00
|
|
|
|
|
|
|
key = cleanKey(key)
|
|
|
|
|
2018-10-24 06:59:40 +00:00
|
|
|
// TODO improve efficiency
|
|
|
|
return false, nil
|
|
|
|
}
|
2018-11-04 19:58:59 +00:00
|
|
|
|
|
|
|
func cleanKey(key string) string {
|
|
|
|
if strings.HasPrefix(key, "/") {
|
|
|
|
key = key[1:]
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|