azure.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // This file is part of the go-ethereum library.
  2. //
  3. // The go-ethereum library is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Lesser General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // The go-ethereum library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU Lesser General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU Lesser General Public License
  14. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  15. package build
  16. import (
  17. "fmt"
  18. "os"
  19. "github.com/Azure/azure-sdk-for-go/storage"
  20. )
  21. // AzureBlobstoreConfig is an authentication and configuration struct containing
  22. // the data needed by the Azure SDK to interact with a speicifc container in the
  23. // blobstore.
  24. type AzureBlobstoreConfig struct {
  25. Account string // Account name to authorize API requests with
  26. Token string // Access token for the above account
  27. Container string // Blob container to upload files into
  28. }
  29. // AzureBlobstoreUpload uploads a local file to the Azure Blob Storage. Note, this
  30. // method assumes a max file size of 64MB (Azure limitation). Larger files will
  31. // need a multi API call approach implemented.
  32. //
  33. // See: https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx#Anchor_3
  34. func AzureBlobstoreUpload(path string, name string, config AzureBlobstoreConfig) error {
  35. if *DryRunFlag {
  36. fmt.Printf("would upload %q to %s/%s/%s\n", path, config.Account, config.Container, name)
  37. return nil
  38. }
  39. // Create an authenticated client against the Azure cloud
  40. rawClient, err := storage.NewBasicClient(config.Account, config.Token)
  41. if err != nil {
  42. return err
  43. }
  44. client := rawClient.GetBlobService()
  45. // Stream the file to upload into the designated blobstore container
  46. in, err := os.Open(path)
  47. if err != nil {
  48. return err
  49. }
  50. defer in.Close()
  51. info, err := in.Stat()
  52. if err != nil {
  53. return err
  54. }
  55. return client.CreateBlockBlobFromReader(config.Container, name, uint64(info.Size()), in, nil)
  56. }