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.

146 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, 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(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(context.Background(),
  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(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(context.Background(), 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(chunk.FileId)
  85. if err != nil {
  86. return err
  87. }
  88. var writeErr error
  89. readErr := util.ReadUrlAsStream(fileUrl, nil, false, chunk.IsFullChunk, chunk.Offset, int(chunk.Size), func(data []byte) {
  90. _, writeErr = appendBlobURL.AppendBlock(context.Background(), 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(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. }