azure.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. "github.com/Azure/azure-sdk-for-go/storage"
  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. }