Added First Storage (Azure File)
This commit is contained in:
parent
39ab159500
commit
daeff4b799
2 changed files with 59 additions and 15 deletions
|
|
@ -1,23 +1,63 @@
|
||||||
package StorageTypes
|
package StorageTypes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"fmt"
|
||||||
|
"github.com/Azure/azure-storage-file-go/azfile"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"scabiosa/Logging"
|
"scabiosa/Logging"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AzureFileStorage struct{
|
type AzureFileStorage struct{
|
||||||
azcopyPath string
|
FileshareName string `json:"fileshareName"`
|
||||||
storageAccUrl string
|
TargetDirectory string `json:"targetDirectory"`
|
||||||
targetDirectory string
|
StorageAccountName string `json:"storageAccountName"`
|
||||||
SASKey string
|
StorageAccountKey string `json:"storageAccountKey"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (azure AzureFileStorage) upload() error{
|
func (azure AzureFileStorage) upload(fileName string){
|
||||||
//Do Stuff here
|
logger := Logging.DetailedLogger("AzureFileStorage", "upload")
|
||||||
return errors.New("lelek")
|
|
||||||
|
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 {
|
func readConfig() []byte {
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,18 @@
|
||||||
package StorageTypes
|
package StorageTypes
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
type Storage interface {
|
type Storage interface {
|
||||||
upload() error
|
upload(fileName string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func UploadFile(storage Storage){
|
func UploadFile(storage Storage, fileName string){
|
||||||
err := storage.upload()
|
storage.upload(fileName)
|
||||||
if err != nil{
|
}
|
||||||
fmt.Print(err)
|
|
||||||
}
|
func CheckStorageType(storageType string) Storage{
|
||||||
|
|
||||||
|
if storageType == "azure-fileshare"{
|
||||||
|
return GetAzureStorage()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
Reference in a new issue