azure.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. "os"
  18. "github.com/Azure/azure-sdk-for-go/storage"
  19. )
  20. // AzureBlobstoreConfig is an authentication and configuration struct containing
  21. // the data needed by the Azure SDK to interact with a speicifc container in the
  22. // blobstore.
  23. type AzureBlobstoreConfig struct {
  24. Account string // Account name to authorize API requests with
  25. Token string // Access token for the above account
  26. Container string // Blob container to upload files into
  27. }
  28. // AzureBlobstoreUpload uploads a local file to the Azure Blob Storage. Note, this
  29. // method assumes a max file size of 64MB (Azure limitation). Larger files will
  30. // need a multi API call approach implemented.
  31. //
  32. // See: https://msdn.microsoft.com/en-us/library/azure/dd179451.aspx#Anchor_3
  33. func AzureBlobstoreUpload(path string, name string, config AzureBlobstoreConfig) error {
  34. // Create an authenticated client against the Azure cloud
  35. rawClient, err := storage.NewBasicClient(config.Account, config.Token)
  36. if err != nil {
  37. return err
  38. }
  39. client := rawClient.GetBlobService()
  40. // Stream the file to upload into the designated blobstore container
  41. in, err := os.Open(path)
  42. if err != nil {
  43. return err
  44. }
  45. defer in.Close()
  46. info, err := in.Stat()
  47. if err != nil {
  48. return err
  49. }
  50. return client.CreateBlockBlobFromReader(config.Container, name, uint64(info.Size()), in, nil)
  51. }