azure.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "os"
  21. "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
  22. )
  23. // AzureBlobstoreConfig is an authentication and configuration struct containing
  24. // the data needed by the Azure SDK to interact with a specific container in the
  25. // blobstore.
  26. type AzureBlobstoreConfig struct {
  27. Account string // Account name to authorize API requests with
  28. Token string // Access token for the above account
  29. Container string // Blob container to upload files into
  30. }
  31. // AzureBlobstoreUpload uploads a local file to the Azure Blob Storage. Note, this
  32. // method assumes a max file size of 64MB (Azure limitation). Larger files will
  33. // need a multi API call approach implemented.
  34. //
  35. // See: https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx#Anchor_3
  36. func AzureBlobstoreUpload(path string, name string, config AzureBlobstoreConfig) error {
  37. if *DryRunFlag {
  38. fmt.Printf("would upload %q to %s/%s/%s\n", path, config.Account, config.Container, name)
  39. return nil
  40. }
  41. // Create an authenticated client against the Azure cloud
  42. credential, err := azblob.NewSharedKeyCredential(config.Account, config.Token)
  43. if err != nil {
  44. return err
  45. }
  46. u := fmt.Sprintf("https://%s.blob.core.windows.net/%s", config.Account, config.Container)
  47. container, err := azblob.NewContainerClientWithSharedKey(u, credential, nil)
  48. if err != nil {
  49. return err
  50. }
  51. // Stream the file to upload into the designated blobstore container
  52. in, err := os.Open(path)
  53. if err != nil {
  54. return err
  55. }
  56. defer in.Close()
  57. blockblob := container.NewBlockBlobClient(name)
  58. _, err = blockblob.Upload(context.Background(), in, nil)
  59. return err
  60. }
  61. // AzureBlobstoreList lists all the files contained within an azure blobstore.
  62. func AzureBlobstoreList(config AzureBlobstoreConfig) ([]*azblob.BlobItemInternal, error) {
  63. // Create an authenticated client against the Azure cloud
  64. credential, err := azblob.NewSharedKeyCredential(config.Account, config.Token)
  65. if err != nil {
  66. return nil, err
  67. }
  68. u := fmt.Sprintf("https://%s.blob.core.windows.net/%s", config.Account, config.Container)
  69. container, err := azblob.NewContainerClientWithSharedKey(u, credential, nil)
  70. if err != nil {
  71. return nil, err
  72. }
  73. var maxResults int32 = 5000
  74. pager := container.ListBlobsFlat(&azblob.ContainerListBlobFlatSegmentOptions{
  75. Maxresults: &maxResults,
  76. })
  77. var allBlobs []*azblob.BlobItemInternal
  78. for pager.NextPage(context.Background()) {
  79. res := pager.PageResponse()
  80. allBlobs = append(allBlobs, res.ContainerListBlobFlatSegmentResult.Segment.BlobItems...)
  81. }
  82. return allBlobs, pager.Err()
  83. }
  84. // AzureBlobstoreDelete iterates over a list of files to delete and removes them
  85. // from the blobstore.
  86. func AzureBlobstoreDelete(config AzureBlobstoreConfig, blobs []*azblob.BlobItemInternal) error {
  87. if *DryRunFlag {
  88. for _, blob := range blobs {
  89. fmt.Printf("would delete %s (%s) from %s/%s\n", *blob.Name, blob.Properties.LastModified, config.Account, config.Container)
  90. }
  91. return nil
  92. }
  93. // Create an authenticated client against the Azure cloud
  94. credential, err := azblob.NewSharedKeyCredential(config.Account, config.Token)
  95. if err != nil {
  96. return err
  97. }
  98. u := fmt.Sprintf("https://%s.blob.core.windows.net/%s", config.Account, config.Container)
  99. container, err := azblob.NewContainerClientWithSharedKey(u, credential, nil)
  100. if err != nil {
  101. return err
  102. }
  103. // Iterate over the blobs and delete them
  104. for _, blob := range blobs {
  105. blockblob := container.NewBlockBlobClient(*blob.Name)
  106. if _, err := blockblob.Delete(context.Background(), &azblob.DeleteBlobOptions{}); err != nil {
  107. return err
  108. }
  109. fmt.Printf("deleted %s (%s)\n", *blob.Name, blob.Properties.LastModified)
  110. }
  111. return nil
  112. }