gcs_sink.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package gcssink
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "cloud.google.com/go/storage"
  7. "google.golang.org/api/option"
  8. "github.com/chrislusf/seaweedfs/weed/filer2"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  12. "github.com/chrislusf/seaweedfs/weed/replication/source"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  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. // Creates a client.
  44. if google_application_credentials == "" {
  45. var found bool
  46. google_application_credentials, found = os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS")
  47. if !found {
  48. glog.Fatalf("need to specific GOOGLE_APPLICATION_CREDENTIALS env variable or google_application_credentials in replication.toml")
  49. }
  50. }
  51. client, err := storage.NewClient(context.Background(), option.WithCredentialsFile(google_application_credentials))
  52. if err != nil {
  53. glog.Fatalf("Failed to create client: %v", err)
  54. }
  55. g.client = client
  56. return nil
  57. }
  58. func (g *GcsSink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool) error {
  59. if isDirectory {
  60. key = key + "/"
  61. }
  62. if err := g.client.Bucket(g.bucket).Object(key).Delete(context.Background()); err != nil {
  63. return fmt.Errorf("gcs delete %s%s: %v", g.bucket, key, err)
  64. }
  65. return nil
  66. }
  67. func (g *GcsSink) CreateEntry(key string, entry *filer_pb.Entry) error {
  68. if entry.IsDirectory {
  69. return nil
  70. }
  71. totalSize := filer2.FileSize(entry)
  72. chunkViews := filer2.ViewFromChunks(g.filerSource.LookupFileId, entry.Chunks, 0, int64(totalSize))
  73. wc := g.client.Bucket(g.bucket).Object(key).NewWriter(context.Background())
  74. for _, chunk := range chunkViews {
  75. fileUrl, err := g.filerSource.LookupFileId(chunk.FileId)
  76. if err != nil {
  77. return err
  78. }
  79. err = util.ReadUrlAsStream(fileUrl, nil, false, chunk.IsFullChunk(), chunk.Offset, int(chunk.Size), func(data []byte) {
  80. wc.Write(data)
  81. })
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. if err := wc.Close(); err != nil {
  87. return err
  88. }
  89. return nil
  90. }
  91. func (g *GcsSink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (foundExistingEntry bool, err error) {
  92. // TODO improve efficiency
  93. return false, nil
  94. }