seaweedfs/weed/replication/sink/gcssink/gcs_sink.go

126 lines
2.9 KiB
Go
Raw Normal View History

2018-10-04 08:14:44 +00:00
package gcssink
import (
"context"
"fmt"
2018-10-06 20:04:33 +00:00
"os"
2018-10-04 08:14:44 +00:00
"cloud.google.com/go/storage"
2018-10-11 07:08:13 +00:00
"github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/chrislusf/seaweedfs/weed/glog"
2018-10-04 08:14:44 +00:00
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
2018-10-11 07:08:13 +00:00
"github.com/chrislusf/seaweedfs/weed/replication/sink"
2018-10-04 08:14:44 +00:00
"github.com/chrislusf/seaweedfs/weed/replication/source"
"github.com/chrislusf/seaweedfs/weed/util"
"google.golang.org/api/option"
)
type GcsSink struct {
client *storage.Client
bucket string
dir string
filerSource *source.FilerSource
}
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
}
func (g *GcsSink) Initialize(configuration util.Configuration, prefix string) error {
2018-10-04 08:14:44 +00:00
return g.initialize(
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
ctx := context.Background()
// Creates a client.
if google_application_credentials == "" {
var found bool
google_application_credentials, found = os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS")
if !found {
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
}
}
client, err := storage.NewClient(ctx, option.WithCredentialsFile(google_application_credentials))
if err != nil {
glog.Fatalf("Failed to create client: %v", err)
2018-10-04 08:14:44 +00:00
}
g.client = client
return nil
}
2019-03-16 00:20:24 +00:00
func (g *GcsSink) DeleteEntry(ctx context.Context, key string, isDirectory, deleteIncludeChunks bool) error {
2018-10-04 08:14:44 +00:00
if isDirectory {
key = key + "/"
}
2019-03-16 00:20:24 +00:00
if err := g.client.Bucket(g.bucket).Object(key).Delete(ctx); 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
}
2019-03-16 00:20:24 +00:00
func (g *GcsSink) CreateEntry(ctx context.Context, key string, entry *filer_pb.Entry) error {
2018-10-04 08:14:44 +00:00
if entry.IsDirectory {
return nil
}
totalSize := filer2.TotalSize(entry.Chunks)
chunkViews := filer2.ViewFromChunks(entry.Chunks, 0, int(totalSize))
wc := g.client.Bucket(g.bucket).Object(key).NewWriter(ctx)
for _, chunk := range chunkViews {
2019-03-16 00:20:24 +00:00
fileUrl, err := g.filerSource.LookupFileId(ctx, chunk.FileId)
2018-10-04 08:14:44 +00:00
if err != nil {
return err
}
_, err = util.ReadUrlAsStream(fileUrl, chunk.Offset, int(chunk.Size), func(data []byte) {
wc.Write(data)
})
if err != nil {
return err
}
}
if err := wc.Close(); err != nil {
return err
}
return nil
}
func (g *GcsSink) UpdateEntry(ctx context.Context, key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (foundExistingEntry bool, err error) {
2018-10-04 08:14:44 +00:00
// TODO improve efficiency
return false, nil
}