From 94a52f98a21acad8e320560efd264e8c7fe6387e Mon Sep 17 00:00:00 2001 From: Xavier Perret <xa.perret@outlook.com> Date: Mon, 17 Oct 2022 16:46:15 +0200 Subject: [PATCH] bim --- app/object-storage/object-storage.go | 48 +++++++++++++++++++++------- app/types/datastructures.go | 17 ++++++++++ 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/app/object-storage/object-storage.go b/app/object-storage/object-storage.go index 229ed5e..258c950 100644 --- a/app/object-storage/object-storage.go +++ b/app/object-storage/object-storage.go @@ -63,11 +63,12 @@ func createBlobStorageClient() { } -func TestObjectStorage() { - fmt.Printf("Azure Blob storage quick start sample\n") +func InitAzureCredentials(storageAccountName string) AzureCredentials { + fmt.Printf("Azure Blob storage initialization\n") - url := "https://hepiadistributedsystems.blob.core.windows.net/" //replace <StorageAccountName> with your Azure storage account name - ctx := context.Background() + //replace <StorageAccountName> with your Azure storage account name + // hepiadistributedsystems + url := "https://" + storageAccountName + ".blob.core.windows.net/" // Create a default request pipeline using your storage account name and account key. credential, err := azidentity.NewDefaultAzureCredential(nil) @@ -80,24 +81,47 @@ func TestObjectStorage() { log.Fatal("Invalid credentials with error: " + err.Error()) } + newAzureCredentials := AzureCredentials{ + Url: url, + Credential: credential, + ServiceClient: serviceClient, + } + return newAzureCredentials +} + +func InitializeObjectStorage(baseObjectStorageName string, azureCreds AzureCredentials) ObjectStorage { + ctx := context.Background() + // Create the container containerName := fmt.Sprintf("quickstart-%s", randomString()) fmt.Printf("Creating a container named %s\n", containerName) - containerClient := serviceClient.NewContainerClient(containerName) - _, err = containerClient.Create(ctx, nil) + containerClient := azureCreds.ServiceClient.NewContainerClient(containerName) + _, err := containerClient.Create(ctx, nil) if err != nil { log.Fatal(err) } - fmt.Printf("Creating a dummy file to test the upload and download\n") - - data := []byte("\nhello world this is a blob\n") - blobName := "quickstartblob" + "-" + randomString() - - blobClient, err := azblob.NewBlockBlobClient(url+containerName+"/"+blobName, credential, nil) + blobClient, err := azblob.NewBlockBlobClient(azureCreds.Url+containerName+"/"+baseObjectStorageName, azureCreds.Credential, nil) if err != nil { log.Fatal(err) } + return ObjectStorage{ + ContainerName: baseObjectStorageName, + ContainerClient: containerClient, + BlockBlobClient: blobClient, + } +} + +func TestObjectStorage() { + // url := "https://hepiadistributedsystems.blob.core.windows.net/" //replace <StorageAccountName> with your Azure storage account name + + azureCreds := InitAzureCredentials("hepiadistributedsystems") + blobName := "quickstartblob" + "-" + randomString() + objectStorage := InitializeObjectStorage(blobName, azureCreds) + + fmt.Printf("Creating a dummy file to test the upload and download\n") + + data := []byte("\nhello world this is a blob\n") // Upload to data to blob storage _, err = blobClient.UploadBufferToBlockBlob(ctx, data, azblob.HighLevelUploadToBlockBlobOption{}) diff --git a/app/types/datastructures.go b/app/types/datastructures.go index 9fd37aa..f31784a 100644 --- a/app/types/datastructures.go +++ b/app/types/datastructures.go @@ -1,5 +1,10 @@ package types +import ( + "github.com/Azure/azure-sdk-for-go/sdk/azidentity" + "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" +) + // Message - MessageBody can be of any type but will be use here for Transaction, AckTransaction type Message struct { MessageType string `json:"messageType"` @@ -22,3 +27,15 @@ type AckTransaction struct { } type Database []Transaction + +type AzureCredentials struct { + Url string + Credential *azidentity.DefaultAzureCredential + ServiceClient azblob.ServiceClient +} + +type ObjectStorage struct { + ContainerName string + ContainerClient azblob.ContainerClient + BlockBlobClient azblob.BlockBlobClient +} -- GitLab