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.

127 lines
2.8 KiB

6 years ago
  1. package gcssink
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "os"
  7. "cloud.google.com/go/storage"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/replication/source"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. "github.com/chrislusf/seaweedfs/weed/filer2"
  12. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  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) error {
  31. return g.initialize(
  32. configuration.GetString("google_application_credentials"),
  33. configuration.GetString("bucket"),
  34. configuration.GetString("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. log.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. log.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) 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", g.bucket, key)
  65. }
  66. return nil
  67. }
  68. func (g *GcsSink) CreateEntry(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. ctx := context.Background()
  75. wc := g.client.Bucket(g.bucket).Object(key).NewWriter(ctx)
  76. for _, chunk := range chunkViews {
  77. fileUrl, err := g.filerSource.LookupFileId(chunk.FileId)
  78. if err != nil {
  79. return err
  80. }
  81. _, err = util.ReadUrlAsStream(fileUrl, chunk.Offset, int(chunk.Size), func(data []byte) {
  82. wc.Write(data)
  83. })
  84. if err != nil {
  85. return err
  86. }
  87. }
  88. if err := wc.Close(); err != nil {
  89. return err
  90. }
  91. return nil
  92. }
  93. func (g *GcsSink) UpdateEntry(key string, oldEntry, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (foundExistingEntry bool, err error) {
  94. // TODO improve efficiency
  95. return false, nil
  96. }