azure_sink.go 3.7 KB

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