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.

133 lines
3.3 KiB

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