|
|
@@ -5,6 +5,7 @@ import (
|
|
|
"context"
|
|
|
"fmt"
|
|
|
"os"
|
|
|
+ "path/filepath"
|
|
|
"strings"
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
|
|
|
@@ -68,32 +69,33 @@ func (sc *StorageConnection) DownloadBlob(blobName string, client *azblob.Client
|
|
|
return downloadedData.Bytes(), nil
|
|
|
}
|
|
|
|
|
|
-// DownloadBlobToFile downloads the Azure Cloud Billing CSV file to a local file
|
|
|
+// DownloadBlobToFile downloads the Azure Billing CSV to a local file
|
|
|
func (sc *StorageConnection) DownloadBlobToFile(localFilePath string, blobName string, client *azblob.Client, ctx context.Context) error {
|
|
|
- log.Infof("Azure: DownloadBlobToFile: retrieving blob: %v", blobName)
|
|
|
-
|
|
|
- // Remove existing
|
|
|
+ // Remove existing Azure Billing CSV on disk
|
|
|
if _, err := os.Stat(localFilePath); err == nil {
|
|
|
err := os.Remove(localFilePath)
|
|
|
if err != nil {
|
|
|
return fmt.Errorf("Azure: DownloadBlobToFile: failed to delete existing file %w", err)
|
|
|
}
|
|
|
- } else {
|
|
|
- return fmt.Errorf("Azure: DownloadBlobToFile: error checking for file %w", err)
|
|
|
}
|
|
|
|
|
|
- // Create new
|
|
|
- file, err := os.Create(localFilePath)
|
|
|
+ // Create filepath
|
|
|
+ dir := filepath.Dir(localFilePath)
|
|
|
+ if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
|
|
+ return fmt.Errorf("Azure: DownloadBlobToFile: failed to create directory %w", err)
|
|
|
+ }
|
|
|
+ fp, err := os.Create(localFilePath)
|
|
|
if err != nil {
|
|
|
return fmt.Errorf("Azure: DownloadBlobToFile: failed to create file %w", err)
|
|
|
}
|
|
|
- defer file.Close()
|
|
|
+ defer fp.Close()
|
|
|
|
|
|
- filesize, err := client.DownloadFile(ctx, sc.Container, blobName, file, nil)
|
|
|
+ // Download newest Azure Billing CSV to disk
|
|
|
+ log.Infof("Azure: DownloadBlobToFile: retrieving blob: %v", blobName)
|
|
|
+ filesize, err := client.DownloadFile(ctx, sc.Container, blobName, fp, nil)
|
|
|
if err != nil {
|
|
|
return fmt.Errorf("Azure: DownloadBlobToFile: failed to download %w", err)
|
|
|
}
|
|
|
-
|
|
|
log.Infof("Azure: DownloadBlobToFile: retrieved %v of size %d", blobName, filesize)
|
|
|
|
|
|
return nil
|