refactor(Azure): Has now separate config struct
This commit is contained in:
parent
aeefe112e0
commit
03ffd77555
2 changed files with 32 additions and 30 deletions
|
|
@ -12,18 +12,17 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"scabiosa/Logging"
|
"scabiosa/Logging"
|
||||||
"scabiosa/SQL"
|
"scabiosa/SQL"
|
||||||
|
"scabiosa/Tools"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AzureFileStorage struct {
|
type AzureFileStorage struct {
|
||||||
FileshareName string `json:"fileshareName"`
|
FileshareName string
|
||||||
TargetDirectory string `json:"targetDirectory"`
|
StorageAccountName string
|
||||||
StorageAccountName string `json:"storageAccountName"`
|
StorageAccountKey string
|
||||||
StorageAccountKey string `json:"storageAccountKey"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (azure AzureFileStorage) upload(fileName string, backupName string, destinationPath string) {
|
func (azure AzureFileStorage) upload(fileName string, backupName string, destinationPath string) {
|
||||||
logger := Logging.DetailedLogger("AzureFileStorage", "upload")
|
logger := Logging.DetailedLogger("AzureFileStorage", "upload")
|
||||||
|
|
||||||
|
|
@ -43,11 +42,7 @@ func (azure AzureFileStorage) upload(fileName string, backupName string, destina
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if destinationPath != ""{
|
u, _ := url.Parse(fmt.Sprintf("https://%s.file.core.windows.net/%s/%s/%s", azure.StorageAccountName, azure.FileshareName, destinationPath, filepath.Base(fileName)))
|
||||||
azure.TargetDirectory = destinationPath
|
|
||||||
}
|
|
||||||
|
|
||||||
u, _ := url.Parse(fmt.Sprintf("https://%s.file.core.windows.net/%s/%s/%s", azure.StorageAccountName, azure.FileshareName ,azure.TargetDirectory, filepath.Base(fileName)))
|
|
||||||
|
|
||||||
fileURL := azfile.NewFileURL(*u, azfile.NewPipeline(credential, azfile.PipelineOptions{}))
|
fileURL := azfile.NewFileURL(*u, azfile.NewPipeline(credential, azfile.PipelineOptions{}))
|
||||||
|
|
||||||
|
|
@ -56,7 +51,6 @@ func (azure AzureFileStorage) upload(fileName string, backupName string, destina
|
||||||
fmt.Printf("[%s] Starting upload to Azure File Share...\n", backupName, ".bak")
|
fmt.Printf("[%s] Starting upload to Azure File Share...\n", backupName, ".bak")
|
||||||
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, SQL.SQLStage_Upload, SQL.REMOTE_AZURE_FILE, "Starting upload.", time.Now())
|
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, SQL.SQLStage_Upload, SQL.REMOTE_AZURE_FILE, "Starting upload.", time.Now())
|
||||||
|
|
||||||
|
|
||||||
progressBar := pb.StartNew(int(fileSize.Size()))
|
progressBar := pb.StartNew(int(fileSize.Size()))
|
||||||
progressBar.Set(pb.Bytes, true)
|
progressBar.Set(pb.Bytes, true)
|
||||||
err = azfile.UploadFileToAzureFile(ctx, file, fileURL,
|
err = azfile.UploadFileToAzureFile(ctx, file, fileURL,
|
||||||
|
|
@ -89,16 +83,20 @@ func readConfig() []byte {
|
||||||
return file
|
return file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func GetAzureStorage() AzureFileStorage {
|
func GetAzureStorage() AzureFileStorage {
|
||||||
logger := Logging.DetailedLogger("AzureFileStorage", "GetAzureStorage")
|
logger := Logging.DetailedLogger("AzureFileStorage", "GetAzureStorage")
|
||||||
|
|
||||||
var azureStorage AzureFileStorage
|
var azureConfig Tools.AzureConfig
|
||||||
|
var azureFileShare AzureFileStorage
|
||||||
|
|
||||||
jsonErr := json.Unmarshal(readConfig(), &azureStorage)
|
jsonErr := json.Unmarshal(readConfig(), &azureConfig)
|
||||||
if jsonErr != nil {
|
if jsonErr != nil {
|
||||||
logger.Fatal(jsonErr)
|
logger.Fatal(jsonErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
return azureStorage
|
azureFileShare.StorageAccountName = azureConfig.StorageAccountName
|
||||||
|
azureFileShare.StorageAccountKey = azureConfig.StorageAccountKey
|
||||||
|
azureFileShare.FileshareName = azureConfig.FileshareName
|
||||||
|
|
||||||
|
return azureFileShare
|
||||||
}
|
}
|
||||||
|
|
@ -16,6 +16,10 @@ type Config struct {
|
||||||
DbUser string `json:"db-user"`
|
DbUser string `json:"db-user"`
|
||||||
DbPassword string `json:"db-password"`
|
DbPassword string `json:"db-password"`
|
||||||
} `json:"sqlConfig"`
|
} `json:"sqlConfig"`
|
||||||
|
type AzureConfig struct {
|
||||||
|
FileshareName string `json:"fileshareName"`
|
||||||
|
StorageAccountName string `json:"storageAccountName"`
|
||||||
|
StorageAccountKey string `json:"storageAccountKey"`
|
||||||
FolderToBackup []struct {
|
FolderToBackup []struct {
|
||||||
BackupName string `json:"backupName"`
|
BackupName string `json:"backupName"`
|
||||||
FolderPath string `json:"folderPath"`
|
FolderPath string `json:"folderPath"`
|
||||||
|
|
|
||||||
Reference in a new issue