2018-10-04 08:14:44 +00:00
|
|
|
package gcssink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/replication/repl_util"
|
2018-10-06 20:04:33 +00:00
|
|
|
"os"
|
2018-10-04 08:14:44 +00:00
|
|
|
|
|
|
|
"cloud.google.com/go/storage"
|
2020-03-06 08:49:47 +00:00
|
|
|
"google.golang.org/api/option"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"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-04 08:14:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type GcsSink struct {
|
2021-03-01 04:34:14 +00:00
|
|
|
client *storage.Client
|
|
|
|
bucket string
|
|
|
|
dir string
|
|
|
|
filerSource *source.FilerSource
|
2021-03-01 00:19:03 +00:00
|
|
|
isIncremental bool
|
2018-10-04 08:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
sink.Sinks = append(sink.Sinks, &GcsSink{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GcsSink) GetName() string {
|
|
|
|
return "google_cloud_storage"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GcsSink) GetSinkToDirectory() string {
|
|
|
|
return g.dir
|
|
|
|
}
|
|
|
|
|
2021-03-01 00:19:03 +00:00
|
|
|
func (g *GcsSink) IsIncremental() bool {
|
|
|
|
return g.isIncremental
|
|
|
|
}
|
|
|
|
|
2020-01-29 17:09:55 +00:00
|
|
|
func (g *GcsSink) Initialize(configuration util.Configuration, prefix string) error {
|
2021-03-01 04:34:14 +00:00
|
|
|
g.isIncremental = configuration.GetBool(prefix + "is_incremental")
|
2018-10-04 08:14:44 +00:00
|
|
|
return g.initialize(
|
2020-01-29 17:09:55 +00:00
|
|
|
configuration.GetString(prefix+"google_application_credentials"),
|
|
|
|
configuration.GetString(prefix+"bucket"),
|
|
|
|
configuration.GetString(prefix+"directory"),
|
2018-10-04 08:14:44 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GcsSink) SetSourceFiler(s *source.FilerSource) {
|
|
|
|
g.filerSource = s
|
|
|
|
}
|
|
|
|
|
2018-10-11 07:08:13 +00:00
|
|
|
func (g *GcsSink) initialize(google_application_credentials, bucketName, dir string) error {
|
2018-10-04 08:14:44 +00:00
|
|
|
g.bucket = bucketName
|
|
|
|
g.dir = dir
|
|
|
|
|
|
|
|
// Creates a client.
|
|
|
|
if google_application_credentials == "" {
|
|
|
|
var found bool
|
|
|
|
google_application_credentials, found = os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS")
|
|
|
|
if !found {
|
2018-11-01 08:11:09 +00:00
|
|
|
glog.Fatalf("need to specific GOOGLE_APPLICATION_CREDENTIALS env variable or google_application_credentials in replication.toml")
|
2018-10-04 08:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 06:23:59 +00:00
|
|
|
client, err := storage.NewClient(context.Background(), option.WithCredentialsFile(google_application_credentials))
|
2018-10-04 08:14:44 +00:00
|
|
|
if err != nil {
|
2018-11-01 08:11:09 +00:00
|
|
|
glog.Fatalf("Failed to create client: %v", err)
|
2018-10-04 08:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g.client = client
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:21:23 +00:00
|
|
|
func (g *GcsSink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) error {
|
2018-10-04 08:14:44 +00:00
|
|
|
|
|
|
|
if isDirectory {
|
|
|
|
key = key + "/"
|
|
|
|
}
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
if err := g.client.Bucket(g.bucket).Object(key).Delete(context.Background()); err != nil {
|
2018-10-09 08:35:30 +00:00
|
|
|
return fmt.Errorf("gcs delete %s%s: %v", g.bucket, key, err)
|
2018-10-04 08:14:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:21:23 +00:00
|
|
|
func (g *GcsSink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) error {
|
2018-10-04 08:14:44 +00:00
|
|
|
|
|
|
|
if entry.IsDirectory {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
totalSize := filer.FileSize(entry)
|
|
|
|
chunkViews := filer.ViewFromChunks(g.filerSource.LookupFileId, entry.Chunks, 0, int64(totalSize))
|
2018-10-04 08:14:44 +00:00
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
wc := g.client.Bucket(g.bucket).Object(key).NewWriter(context.Background())
|
2020-10-08 05:49:04 +00:00
|
|
|
defer wc.Close()
|
2018-10-04 08:14:44 +00:00
|
|
|
|
2020-10-08 05:49:04 +00:00
|
|
|
writeFunc := func(data []byte) error {
|
|
|
|
_, writeErr := wc.Write(data)
|
|
|
|
return writeErr
|
2018-10-04 08:14:44 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 05:49:04 +00:00
|
|
|
if err := repl_util.CopyFromChunkViews(chunkViews, g.filerSource, writeFunc); err != nil {
|
2018-10-04 08:14:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:21:23 +00:00
|
|
|
func (g *GcsSink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool, signatures []int32) (foundExistingEntry bool, err error) {
|
2021-05-26 21:53:11 +00:00
|
|
|
return true, g.CreateEntry(key, newEntry, signatures)
|
2018-10-04 08:14:44 +00:00
|
|
|
}
|