azure.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package build
  17. import (
  18. "context"
  19. "fmt"
  20. "net/url"
  21. "os"
  22. "github.com/Azure/azure-storage-blob-go/azblob"
  23. )
  24. // AzureBlobstoreConfig is an authentication and configuration struct containing
  25. // the data needed by the Azure SDK to interact with a specific container in the
  26. // blobstore.
  27. type AzureBlobstoreConfig struct {
  28. Account string // Account name to authorize API requests with
  29. Token string // Access token for the above account
  30. Container string // Blob container to upload files into
  31. }
  32. // AzureBlobstoreUpload uploads a local file to the Azure Blob Storage. Note, this
  33. // method assumes a max file size of 64MB (Azure limitation). Larger files will
  34. // need a multi API call approach implemented.
  35. //
  36. // See: https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx#Anchor_3
  37. func AzureBlobstoreUpload(path string, name string, config AzureBlobstoreConfig) error {
  38. if *DryRunFlag {
  39. fmt.Printf("would upload %q to %s/%s/%s\n", path, config.Account, config.Container, name)
  40. return nil
  41. }
  42. // Create an authenticated client against the Azure cloud
  43. credential, err := azblob.NewSharedKeyCredential(config.Account, config.Token)
  44. if err != nil {
  45. return err
  46. }
  47. pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{})
  48. u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", config.Account))
  49. service := azblob.NewServiceURL(*u, pipeline)
  50. container := service.NewContainerURL(config.Container)
  51. blockblob := container.NewBlockBlobURL(name)
  52. // Stream the file to upload into the designated blobstore container
  53. in, err := os.Open(path)
  54. if err != nil {
  55. return err
  56. }
  57. defer in.Close()
  58. _, err = blockblob.Upload(context.Background(), in, azblob.BlobHTTPHeaders{}, azblob.Metadata{}, azblob.BlobAccessConditions{})
  59. return err
  60. }
  61. // AzureBlobstoreList lists all the files contained within an azure blobstore.
  62. func AzureBlobstoreList(config AzureBlobstoreConfig) ([]azblob.BlobItem, error) {
  63. credential := azblob.NewAnonymousCredential()
  64. if len(config.Token) > 0 {
  65. c, err := azblob.NewSharedKeyCredential(config.Account, config.Token)
  66. if err != nil {
  67. return nil, err
  68. }
  69. credential = c
  70. }
  71. pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{})
  72. u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", config.Account))
  73. service := azblob.NewServiceURL(*u, pipeline)
  74. var allBlobs []azblob.BlobItem
  75. // List all the blobs from the container and return them
  76. container := service.NewContainerURL(config.Container)
  77. nextMarker := azblob.Marker{}
  78. for nextMarker.NotDone() {
  79. res, err := container.ListBlobsFlatSegment(context.Background(), nextMarker, azblob.ListBlobsSegmentOptions{
  80. MaxResults: 5000, // The server only gives max 5K items
  81. })
  82. if err != nil {
  83. return nil, err
  84. }
  85. allBlobs = append(allBlobs, res.Segment.BlobItems...)
  86. nextMarker = res.NextMarker
  87. }
  88. return allBlobs, nil
  89. }
  90. // AzureBlobstoreDelete iterates over a list of files to delete and removes them
  91. // from the blobstore.
  92. func AzureBlobstoreDelete(config AzureBlobstoreConfig, blobs []azblob.BlobItem) error {
  93. if *DryRunFlag {
  94. for _, blob := range blobs {
  95. fmt.Printf("would delete %s (%s) from %s/%s\n", blob.Name, blob.Properties.LastModified, config.Account, config.Container)
  96. }
  97. return nil
  98. }
  99. // Create an authenticated client against the Azure cloud
  100. credential, err := azblob.NewSharedKeyCredential(config.Account, config.Token)
  101. if err != nil {
  102. return err
  103. }
  104. pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{})
  105. u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", config.Account))
  106. service := azblob.NewServiceURL(*u, pipeline)
  107. container := service.NewContainerURL(config.Container)
  108. // Iterate over the blobs and delete them
  109. for _, blob := range blobs {
  110. blockblob := container.NewBlockBlobURL(blob.Name)
  111. if _, err := blockblob.Delete(context.Background(), azblob.DeleteSnapshotsOptionInclude, azblob.BlobAccessConditions{}); err != nil {
  112. return err
  113. }
  114. fmt.Printf("deleted %s (%s)\n", blob.Name, blob.Properties.LastModified)
  115. }
  116. return nil
  117. }