storageconnection.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package azure
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "net/url"
  7. "strings"
  8. "github.com/Azure/azure-storage-blob-go/azblob"
  9. "github.com/opencost/opencost/pkg/cloud"
  10. cloudconfig "github.com/opencost/opencost/pkg/cloud/config"
  11. "github.com/opencost/opencost/pkg/log"
  12. )
  13. // StorageConnection provides access to Azure Storage
  14. type StorageConnection struct {
  15. StorageConfiguration
  16. ConnectionStatus cloud.ConnectionStatus
  17. }
  18. func (sc *StorageConnection) GetStatus() cloud.ConnectionStatus {
  19. return sc.ConnectionStatus
  20. }
  21. func (sc *StorageConnection) Equals(config cloudconfig.Config) bool {
  22. thatConfig, ok := config.(*StorageConnection)
  23. if !ok {
  24. return false
  25. }
  26. return sc.StorageConfiguration.Equals(&thatConfig.StorageConfiguration)
  27. }
  28. func (sc *StorageConnection) getContainer() (*azblob.ContainerURL, error) {
  29. credential, err := sc.Authorizer.GetBlobCredentials()
  30. if err != nil {
  31. return nil, err
  32. }
  33. p := azblob.NewPipeline(credential, azblob.PipelineOptions{})
  34. // From the Azure portal, get your storage account blob service URL endpoint.
  35. URL, _ := url.Parse(
  36. fmt.Sprintf(sc.getBlobURLTemplate(), sc.Account, sc.Container))
  37. // Create a ContainerURL object that wraps the container URL and a request
  38. // pipeline to make requests.
  39. containerURL := azblob.NewContainerURL(*URL, p)
  40. return &containerURL, nil
  41. }
  42. // getBlobURLTemplate returns the correct BlobUrl for whichever Cloud storage account is specified by the AzureCloud configuration
  43. // defaults to the Public Cloud template
  44. func (sc *StorageConnection) getBlobURLTemplate() string {
  45. // Use gov cloud blob url if gov is detected in AzureCloud
  46. if strings.Contains(strings.ToLower(sc.Cloud), "gov") {
  47. return "https://%s.blob.core.usgovcloudapi.net/%s"
  48. }
  49. // default to Public Cloud template
  50. return "https://%s.blob.core.windows.net/%s"
  51. }
  52. func (sc *StorageConnection) DownloadBlob(blobName string, containerURL *azblob.ContainerURL, ctx context.Context) ([]byte, error) {
  53. log.Infof("Azure Storage: retrieving blob: %v", blobName)
  54. blobURL := containerURL.NewBlobURL(blobName)
  55. downloadResponse, err := blobURL.Download(ctx, 0, azblob.CountToEnd, azblob.BlobAccessConditions{}, false, azblob.ClientProvidedKeyOptions{})
  56. if err != nil {
  57. return nil, err
  58. }
  59. // NOTE: automatically retries are performed if the connection fails
  60. bodyStream := downloadResponse.Body(azblob.RetryReaderOptions{MaxRetryRequests: 20})
  61. // read the body into a buffer
  62. downloadedData := bytes.Buffer{}
  63. _, err = downloadedData.ReadFrom(bodyStream)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return downloadedData.Bytes(), nil
  68. }