You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.9 KiB

6 years ago
6 years ago
6 years ago
  1. package gcssink
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/replication/repl_util"
  6. "os"
  7. "cloud.google.com/go/storage"
  8. "google.golang.org/api/option"
  9. "github.com/chrislusf/seaweedfs/weed/filer"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  13. "github.com/chrislusf/seaweedfs/weed/replication/source"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. )
  16. type GcsSink struct {
  17. client *storage.Client
  18. bucket string
  19. dir string
  20. filerSource *source.FilerSource
  21. }
  22. func init() {
  23. sink.Sinks = append(sink.Sinks, &GcsSink{})
  24. }
  25. func (g *GcsSink) GetName() string {
  26. return "google_cloud_storage"
  27. }
  28. func (g *GcsSink) GetSinkToDirectory() string {
  29. return g.dir
  30. }
  31. func (g *GcsSink) Initialize(configuration util.Configuration, prefix string) error {
  32. return g.initialize(
  33. configuration.GetString(prefix+"google_application_credentials"),
  34. configuration.GetString(prefix+"bucket"),
  35. configuration.GetString(prefix+"directory"),
  36. )
  37. }
  38. func (g *GcsSink) SetSourceFiler(s *source.FilerSource) {
  39. g.filerSource = s
  40. }
  41. func (g *GcsSink) initialize(google_application_credentials, bucketName, dir string) error {
  42. g.bucket = bucketName
  43. g.dir = dir
  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(context.Background(), 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(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) error {
  60. if isDirectory {
  61. key = key + "/"
  62. }
  63. if err := g.client.Bucket(g.bucket).Object(key).Delete(context.Background()); 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(key string, entry *filer_pb.Entry, signatures []int32) error {
  69. if entry.IsDirectory {
  70. return nil
  71. }
  72. totalSize := filer.FileSize(entry)
  73. chunkViews := filer.ViewFromChunks(g.filerSource.LookupFileId, entry.Chunks, 0, int64(totalSize))
  74. wc := g.client.Bucket(g.bucket).Object(key).NewWriter(context.Background())
  75. defer wc.Close()
  76. writeFunc := func(data []byte) error {
  77. _, writeErr := wc.Write(data)
  78. return writeErr
  79. }
  80. if err := repl_util.CopyFromChunkViews(chunkViews, g.filerSource, writeFunc); err != nil {
  81. return err
  82. }
  83. return nil
  84. }
  85. func (g *GcsSink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool, signatures []int32) (foundExistingEntry bool, err error) {
  86. // TODO improve efficiency
  87. return false, nil
  88. }