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.

135 lines
3.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package azuresink
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/replication/repl_util"
  7. "net/url"
  8. "strings"
  9. "github.com/Azure/azure-storage-blob-go/azblob"
  10. "github.com/chrislusf/seaweedfs/weed/filer"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  13. "github.com/chrislusf/seaweedfs/weed/replication/sink"
  14. "github.com/chrislusf/seaweedfs/weed/replication/source"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. type AzureSink struct {
  18. containerURL azblob.ContainerURL
  19. container string
  20. dir string
  21. filerSource *source.FilerSource
  22. }
  23. func init() {
  24. sink.Sinks = append(sink.Sinks, &AzureSink{})
  25. }
  26. func (g *AzureSink) GetName() string {
  27. return "azure"
  28. }
  29. func (g *AzureSink) GetSinkToDirectory() string {
  30. return g.dir
  31. }
  32. func (g *AzureSink) Initialize(configuration util.Configuration, prefix string) error {
  33. return g.initialize(
  34. configuration.GetString(prefix+"account_name"),
  35. configuration.GetString(prefix+"account_key"),
  36. configuration.GetString(prefix+"container"),
  37. configuration.GetString(prefix+"directory"),
  38. )
  39. }
  40. func (g *AzureSink) SetSourceFiler(s *source.FilerSource) {
  41. g.filerSource = s
  42. }
  43. func (g *AzureSink) initialize(accountName, accountKey, container, dir string) error {
  44. g.container = container
  45. g.dir = dir
  46. // Use your Storage account's name and key to create a credential object.
  47. credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
  48. if err != nil {
  49. glog.Fatalf("failed to create Azure credential with account name:%s key:%s", accountName, accountKey)
  50. }
  51. // Create a request pipeline that is used to process HTTP(S) requests and responses.
  52. p := azblob.NewPipeline(credential, azblob.PipelineOptions{})
  53. // Create an ServiceURL object that wraps the service URL and a request pipeline.
  54. u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", accountName))
  55. serviceURL := azblob.NewServiceURL(*u, p)
  56. g.containerURL = serviceURL.NewContainerURL(g.container)
  57. return nil
  58. }
  59. func (g *AzureSink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) error {
  60. key = cleanKey(key)
  61. if isDirectory {
  62. key = key + "/"
  63. }
  64. if _, err := g.containerURL.NewBlobURL(key).Delete(context.Background(),
  65. azblob.DeleteSnapshotsOptionInclude, azblob.BlobAccessConditions{}); err != nil {
  66. return fmt.Errorf("azure delete %s/%s: %v", g.container, key, err)
  67. }
  68. return nil
  69. }
  70. func (g *AzureSink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) error {
  71. key = cleanKey(key)
  72. if entry.IsDirectory {
  73. return nil
  74. }
  75. totalSize := filer.FileSize(entry)
  76. chunkViews := filer.ViewFromChunks(g.filerSource.LookupFileId, entry.Chunks, 0, int64(totalSize))
  77. // Create a URL that references a to-be-created blob in your
  78. // Azure Storage account's container.
  79. appendBlobURL := g.containerURL.NewAppendBlobURL(key)
  80. _, err := appendBlobURL.Create(context.Background(), azblob.BlobHTTPHeaders{}, azblob.Metadata{}, azblob.BlobAccessConditions{})
  81. if err != nil {
  82. return err
  83. }
  84. writeFunc := func(data []byte) error {
  85. _, writeErr := appendBlobURL.AppendBlock(context.Background(), bytes.NewReader(data), azblob.AppendBlobAccessConditions{}, nil)
  86. return writeErr
  87. }
  88. if err := repl_util.CopyFromChunkViews(chunkViews, g.filerSource, writeFunc); err != nil {
  89. return err
  90. }
  91. return nil
  92. }
  93. func (g *AzureSink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool, signatures []int32) (foundExistingEntry bool, err error) {
  94. key = cleanKey(key)
  95. // TODO improve efficiency
  96. return false, nil
  97. }
  98. func cleanKey(key string) string {
  99. if strings.HasPrefix(key, "/") {
  100. key = key[1:]
  101. }
  102. return key
  103. }