From daeff4b7991fdfdf00cb049f007e907166cdd4ad Mon Sep 17 00:00:00 2001 From: netbenix Date: Fri, 19 Nov 2021 19:36:54 +0100 Subject: [PATCH] Added First Storage (Azure File) --- StorageTypes/AzureFileStorage.go | 56 +++++++++++++++++++++++++++----- StorageTypes/StorageInterface.go | 18 ++++++---- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/StorageTypes/AzureFileStorage.go b/StorageTypes/AzureFileStorage.go index 6799f8e..c4a9387 100644 --- a/StorageTypes/AzureFileStorage.go +++ b/StorageTypes/AzureFileStorage.go @@ -1,23 +1,63 @@ package StorageTypes import ( + "context" "encoding/json" - "errors" + "fmt" + "github.com/Azure/azure-storage-file-go/azfile" + "net/url" "os" + "path/filepath" "scabiosa/Logging" + "strings" ) type AzureFileStorage struct{ - azcopyPath string - storageAccUrl string - targetDirectory string - SASKey string + FileshareName string `json:"fileshareName"` + TargetDirectory string `json:"targetDirectory"` + StorageAccountName string `json:"storageAccountName"` + StorageAccountKey string `json:"storageAccountKey"` } -func (azure AzureFileStorage) upload() error{ - //Do Stuff here - return errors.New("lelek") +func (azure AzureFileStorage) upload(fileName string){ + logger := Logging.DetailedLogger("AzureFileStorage", "upload") + + file, err := os.Open(fileName) + if err != nil { + logger.Fatal(err) + } + defer file.Close() + + fileSize, err := file.Stat() + if err != nil { + logger.Fatal(err) + } + + credential, err := azfile.NewSharedKeyCredential(azure.StorageAccountName, azure.StorageAccountKey) + if err != nil{ + logger.Fatal(err) + } + + 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{})) + + ctx := context.Background() + + fmt.Printf("[%s] Starting upload to Azure File Share...\n", strings.Trim(filepath.Base(fileName), ".bak")) + + err = azfile.UploadFileToAzureFile(ctx, file, fileURL, + azfile.UploadToAzureFileOptions{ + Parallelism: 3, + FileHTTPHeaders: azfile.FileHTTPHeaders{ + CacheControl: "no-transform", + }, + Progress: func(bytesTransferred int64){ + fmt.Printf("[%s] Uploaded %d of %d bytes.\n", strings.Trim(filepath.Base(fileName), ".bak") ,bytesTransferred, fileSize.Size()) + }}) + + fmt.Printf("[%s] Upload finished.\n", strings.Trim(filepath.Base(fileName), ".bak")) } func readConfig() []byte { diff --git a/StorageTypes/StorageInterface.go b/StorageTypes/StorageInterface.go index 9346b35..b85a5dd 100644 --- a/StorageTypes/StorageInterface.go +++ b/StorageTypes/StorageInterface.go @@ -1,14 +1,18 @@ package StorageTypes -import "fmt" - type Storage interface { - upload() error + upload(fileName string) } -func UploadFile(storage Storage){ - err := storage.upload() - if err != nil{ - fmt.Print(err) +func UploadFile(storage Storage, fileName string){ + storage.upload(fileName) +} + +func CheckStorageType(storageType string) Storage{ + + if storageType == "azure-fileshare"{ + return GetAzureStorage() } + + return nil } \ No newline at end of file