azure.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 speicifc 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, err := azblob.NewSharedKeyCredential(config.Account, config.Token)
  64. if err != nil {
  65. return nil, err
  66. }
  67. pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{})
  68. u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", config.Account))
  69. service := azblob.NewServiceURL(*u, pipeline)
  70. // List all the blobs from the container and return them
  71. container := service.NewContainerURL(config.Container)
  72. res, err := container.ListBlobsFlatSegment(context.Background(), azblob.Marker{}, azblob.ListBlobsSegmentOptions{
  73. MaxResults: 1024 * 1024 * 1024, // Yes, fetch all of them
  74. })
  75. if err != nil {
  76. return nil, err
  77. }
  78. return res.Segment.BlobItems, nil
  79. }
  80. // AzureBlobstoreDelete iterates over a list of files to delete and removes them
  81. // from the blobstore.
  82. func AzureBlobstoreDelete(config AzureBlobstoreConfig, blobs []azblob.BlobItem) error {
  83. if *DryRunFlag {
  84. for _, blob := range blobs {
  85. fmt.Printf("would delete %s (%s) from %s/%s\n", blob.Name, blob.Properties.LastModified, config.Account, config.Container)
  86. }
  87. return nil
  88. }
  89. // Create an authenticated client against the Azure cloud
  90. credential, err := azblob.NewSharedKeyCredential(config.Account, config.Token)
  91. if err != nil {
  92. return err
  93. }
  94. pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{})
  95. u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", config.Account))
  96. service := azblob.NewServiceURL(*u, pipeline)
  97. container := service.NewContainerURL(config.Container)
  98. // Iterate over the blobs and delete them
  99. for _, blob := range blobs {
  100. blockblob := container.NewBlockBlobURL(blob.Name)
  101. if _, err := blockblob.Delete(context.Background(), azblob.DeleteSnapshotsOptionInclude, azblob.BlobAccessConditions{}); err != nil {
  102. return err
  103. }
  104. }
  105. return nil
  106. }