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.

150 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
6 years ago
  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) error {
  32. return g.initialize(
  33. configuration.GetString("account_name"),
  34. configuration.GetString("account_key"),
  35. configuration.GetString("container"),
  36. configuration.GetString("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(key string, isDirectory, deleteIncludeChunks bool) error {
  59. key = cleanKey(key)
  60. if isDirectory {
  61. key = key + "/"
  62. }
  63. ctx := context.Background()
  64. if _, err := g.containerURL.NewBlobURL(key).Delete(ctx,
  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) error {
  71. key = cleanKey(key)
  72. if entry.IsDirectory {
  73. return nil
  74. }
  75. totalSize := filer2.TotalSize(entry.Chunks)
  76. chunkViews := filer2.ViewFromChunks(entry.Chunks, 0, int(totalSize))
  77. ctx := context.Background()
  78. // Create a URL that references a to-be-created blob in your
  79. // Azure Storage account's container.
  80. appendBlobURL := g.containerURL.NewAppendBlobURL(key)
  81. _, err := appendBlobURL.Create(ctx, azblob.BlobHTTPHeaders{}, azblob.Metadata{}, azblob.BlobAccessConditions{})
  82. if err != nil {
  83. return err
  84. }
  85. for _, chunk := range chunkViews {
  86. fileUrl, err := g.filerSource.LookupFileId(chunk.FileId)
  87. if err != nil {
  88. return err
  89. }
  90. var writeErr error
  91. _, readErr := util.ReadUrlAsStream(fileUrl, chunk.Offset, int(chunk.Size), func(data []byte) {
  92. _, writeErr = appendBlobURL.AppendBlock(ctx, bytes.NewReader(data), azblob.AppendBlobAccessConditions{}, nil)
  93. })
  94. if readErr != nil {
  95. return readErr
  96. }
  97. if writeErr != nil {
  98. return writeErr
  99. }
  100. }
  101. return nil
  102. }
  103. func (g *AzureSink) UpdateEntry(key string, oldEntry, newEntry *filer_pb.Entry, deleteIncludeChunks bool) (foundExistingEntry bool, err error) {
  104. key = cleanKey(key)
  105. // TODO improve efficiency
  106. return false, nil
  107. }
  108. func cleanKey(key string) string {
  109. if strings.HasPrefix(key, "/") {
  110. key = key[1:]
  111. }
  112. return key
  113. }