azure_sink.go 4.4 KB

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