azure.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "fmt"
  19. "os"
  20. storage "github.com/Azure/azure-storage-go"
  21. )
  22. // AzureBlobstoreConfig is an authentication and configuration struct containing
  23. // the data needed by the Azure SDK to interact with a speicifc container in the
  24. // blobstore.
  25. type AzureBlobstoreConfig struct {
  26. Account string // Account name to authorize API requests with
  27. Token string // Access token for the above account
  28. Container string // Blob container to upload files into
  29. }
  30. // AzureBlobstoreUpload uploads a local file to the Azure Blob Storage. Note, this
  31. // method assumes a max file size of 64MB (Azure limitation). Larger files will
  32. // need a multi API call approach implemented.
  33. //
  34. // See: https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx#Anchor_3
  35. func AzureBlobstoreUpload(path string, name string, config AzureBlobstoreConfig) error {
  36. if *DryRunFlag {
  37. fmt.Printf("would upload %q to %s/%s/%s\n", path, config.Account, config.Container, name)
  38. return nil
  39. }
  40. // Create an authenticated client against the Azure cloud
  41. rawClient, err := storage.NewBasicClient(config.Account, config.Token)
  42. if err != nil {
  43. return err
  44. }
  45. client := rawClient.GetBlobService()
  46. // Stream the file to upload into the designated blobstore container
  47. in, err := os.Open(path)
  48. if err != nil {
  49. return err
  50. }
  51. defer in.Close()
  52. info, err := in.Stat()
  53. if err != nil {
  54. return err
  55. }
  56. return client.CreateBlockBlobFromReader(config.Container, name, uint64(info.Size()), in, nil)
  57. }
  58. // AzureBlobstoreList lists all the files contained within an azure blobstore.
  59. func AzureBlobstoreList(config AzureBlobstoreConfig) ([]storage.Blob, error) {
  60. // Create an authenticated client against the Azure cloud
  61. rawClient, err := storage.NewBasicClient(config.Account, config.Token)
  62. if err != nil {
  63. return nil, err
  64. }
  65. client := rawClient.GetBlobService()
  66. // List all the blobs from the container and return them
  67. container := client.GetContainerReference(config.Container)
  68. blobs, err := container.ListBlobs(storage.ListBlobsParameters{
  69. MaxResults: 1024 * 1024 * 1024, // Yes, fetch all of them
  70. Timeout: 3600, // Yes, wait for all of them
  71. })
  72. if err != nil {
  73. return nil, err
  74. }
  75. return blobs.Blobs, nil
  76. }
  77. // AzureBlobstoreDelete iterates over a list of files to delete and removes them
  78. // from the blobstore.
  79. func AzureBlobstoreDelete(config AzureBlobstoreConfig, blobs []storage.Blob) error {
  80. if *DryRunFlag {
  81. for _, blob := range blobs {
  82. fmt.Printf("would delete %s (%s) from %s/%s\n", blob.Name, blob.Properties.LastModified, config.Account, config.Container)
  83. }
  84. return nil
  85. }
  86. // Create an authenticated client against the Azure cloud
  87. rawClient, err := storage.NewBasicClient(config.Account, config.Token)
  88. if err != nil {
  89. return err
  90. }
  91. client := rawClient.GetBlobService()
  92. // Iterate over the blobs and delete them
  93. for _, blob := range blobs {
  94. if err := client.DeleteBlob(config.Container, blob.Name, nil); err != nil {
  95. return err
  96. }
  97. }
  98. return nil
  99. }