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.

125 lines
2.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  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) 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. 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, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (foundExistingEntry bool, err error) {
  93. // TODO improve efficiency
  94. return false, nil
  95. }