2018-10-04 16:14:44 +08:00
|
|
|
package gcssink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-10-07 04:04:33 +08:00
|
|
|
"os"
|
2018-10-04 16:14:44 +08:00
|
|
|
|
|
|
|
"cloud.google.com/go/storage"
|
2020-03-06 16:49:47 +08:00
|
|
|
"google.golang.org/api/option"
|
|
|
|
|
2018-10-11 15:08:13 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
2018-11-01 16:11:09 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-10-04 16:14:44 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-10-11 15:08:13 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/replication/sink"
|
2018-10-04 16:14:44 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/replication/source"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-30 01:09:55 +08:00
|
|
|
func (g *GcsSink) Initialize(configuration util.Configuration, prefix string) error {
|
2018-10-04 16:14:44 +08:00
|
|
|
return g.initialize(
|
2020-01-30 01:09:55 +08:00
|
|
|
configuration.GetString(prefix+"google_application_credentials"),
|
|
|
|
configuration.GetString(prefix+"bucket"),
|
|
|
|
configuration.GetString(prefix+"directory"),
|
2018-10-04 16:14:44 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GcsSink) SetSourceFiler(s *source.FilerSource) {
|
|
|
|
g.filerSource = s
|
|
|
|
}
|
|
|
|
|
2018-10-11 15:08:13 +08:00
|
|
|
func (g *GcsSink) initialize(google_application_credentials, bucketName, dir string) error {
|
2018-10-04 16:14:44 +08: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 16:11:09 +08:00
|
|
|
glog.Fatalf("need to specific GOOGLE_APPLICATION_CREDENTIALS env variable or google_application_credentials in replication.toml")
|
2018-10-04 16:14:44 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-26 14:23:59 +08:00
|
|
|
client, err := storage.NewClient(context.Background(), option.WithCredentialsFile(google_application_credentials))
|
2018-10-04 16:14:44 +08:00
|
|
|
if err != nil {
|
2018-11-01 16:11:09 +08:00
|
|
|
glog.Fatalf("Failed to create client: %v", err)
|
2018-10-04 16:14:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
g.client = client
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
func (g *GcsSink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool) error {
|
2018-10-04 16:14:44 +08:00
|
|
|
|
|
|
|
if isDirectory {
|
|
|
|
key = key + "/"
|
|
|
|
}
|
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
if err := g.client.Bucket(g.bucket).Object(key).Delete(context.Background()); err != nil {
|
2018-10-09 16:35:30 +08:00
|
|
|
return fmt.Errorf("gcs delete %s%s: %v", g.bucket, key, err)
|
2018-10-04 16:14:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
func (g *GcsSink) CreateEntry(key string, entry *filer_pb.Entry) error {
|
2018-10-04 16:14:44 +08:00
|
|
|
|
|
|
|
if entry.IsDirectory {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-16 00:32:47 +08:00
|
|
|
totalSize := filer2.FileSize(entry)
|
2020-07-20 08:59:43 +08:00
|
|
|
chunkViews := filer2.ViewFromChunks(g.filerSource.LookupFileId, entry.Chunks, 0, int64(totalSize))
|
2018-10-04 16:14:44 +08:00
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
wc := g.client.Bucket(g.bucket).Object(key).NewWriter(context.Background())
|
2018-10-04 16:14:44 +08:00
|
|
|
|
|
|
|
for _, chunk := range chunkViews {
|
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
fileUrl, err := g.filerSource.LookupFileId(chunk.FileId)
|
2018-10-04 16:14:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-14 12:58:10 +08:00
|
|
|
err = util.ReadUrlAsStream(fileUrl, nil, false, chunk.IsFullChunk(), chunk.Offset, int(chunk.Size), func(data []byte) {
|
2018-10-04 16:14:44 +08:00
|
|
|
wc.Write(data)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := wc.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
func (g *GcsSink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (foundExistingEntry bool, err error) {
|
2018-10-04 16:14:44 +08:00
|
|
|
// TODO improve efficiency
|
|
|
|
return false, nil
|
|
|
|
}
|