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.

130 lines
2.9 KiB

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