azure_sink.go 3.9 KB

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