gcs_sink.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package gcssink
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "cloud.google.com/go/storage"
  7. "github.com/chrislusf/seaweedfs/weed/filer2"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  11. "github.com/chrislusf/seaweedfs/weed/replication/source"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. "google.golang.org/api/option"
  14. )
  15. type GcsSink struct {
  16. client *storage.Client
  17. bucket string
  18. dir string
  19. filerSource *source.FilerSource
  20. }
  21. func init() {
  22. sink.Sinks = append(sink.Sinks, &GcsSink{})
  23. }
  24. func (g *GcsSink) GetName() string {
  25. return "google_cloud_storage"
  26. }
  27. func (g *GcsSink) GetSinkToDirectory() string {
  28. return g.dir
  29. }
  30. func (g *GcsSink) Initialize(configuration util.Configuration, prefix string) error {
  31. return g.initialize(
  32. configuration.GetString(prefix+"google_application_credentials"),
  33. configuration.GetString(prefix+"bucket"),
  34. configuration.GetString(prefix+"directory"),
  35. )
  36. }
  37. func (g *GcsSink) SetSourceFiler(s *source.FilerSource) {
  38. g.filerSource = s
  39. }
  40. func (g *GcsSink) initialize(google_application_credentials, bucketName, dir string) error {
  41. g.bucket = bucketName
  42. g.dir = dir
  43. ctx := context.Background()
  44. // Creates a client.
  45. if google_application_credentials == "" {
  46. var found bool
  47. google_application_credentials, found = os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS")
  48. if !found {
  49. glog.Fatalf("need to specific GOOGLE_APPLICATION_CREDENTIALS env variable or google_application_credentials in replication.toml")
  50. }
  51. }
  52. client, err := storage.NewClient(ctx, option.WithCredentialsFile(google_application_credentials))
  53. if err != nil {
  54. glog.Fatalf("Failed to create client: %v", err)
  55. }
  56. g.client = client
  57. return nil
  58. }
  59. func (g *GcsSink) DeleteEntry(ctx context.Context, key string, isDirectory, deleteIncludeChunks bool) error {
  60. if isDirectory {
  61. key = key + "/"
  62. }
  63. if err := g.client.Bucket(g.bucket).Object(key).Delete(ctx); err != nil {
  64. return fmt.Errorf("gcs delete %s%s: %v", g.bucket, key, err)
  65. }
  66. return nil
  67. }
  68. func (g *GcsSink) CreateEntry(ctx context.Context, key string, entry *filer_pb.Entry) error {
  69. if entry.IsDirectory {
  70. return nil
  71. }
  72. totalSize := filer2.TotalSize(entry.Chunks)
  73. chunkViews := filer2.ViewFromChunks(entry.Chunks, 0, int(totalSize))
  74. wc := g.client.Bucket(g.bucket).Object(key).NewWriter(ctx)
  75. for _, chunk := range chunkViews {
  76. fileUrl, err := g.filerSource.LookupFileId(ctx, chunk.FileId)
  77. if err != nil {
  78. return err
  79. }
  80. _, err = util.ReadUrlAsStream(fileUrl, chunk.Offset, int(chunk.Size), func(data []byte) {
  81. wc.Write(data)
  82. })
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. if err := wc.Close(); err != nil {
  88. return err
  89. }
  90. return nil
  91. }
  92. 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) {
  93. // TODO improve efficiency
  94. return false, nil
  95. }