storageconnection.go 2.3 KB

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