Skip to content
Snippets Groups Projects
Commit 94a52f98 authored by Xavier Perret's avatar Xavier Perret
Browse files

bim

parent 7ee8289c
Branches
No related tags found
1 merge request!2added file to separate client function from the server
...@@ -63,11 +63,12 @@ func createBlobStorageClient() { ...@@ -63,11 +63,12 @@ func createBlobStorageClient() {
} }
func TestObjectStorage() { func InitAzureCredentials(storageAccountName string) AzureCredentials {
fmt.Printf("Azure Blob storage quick start sample\n") fmt.Printf("Azure Blob storage initialization\n")
url := "https://hepiadistributedsystems.blob.core.windows.net/" //replace <StorageAccountName> with your Azure storage account name //replace <StorageAccountName> with your Azure storage account name
ctx := context.Background() // hepiadistributedsystems
url := "https://" + storageAccountName + ".blob.core.windows.net/"
// Create a default request pipeline using your storage account name and account key. // Create a default request pipeline using your storage account name and account key.
credential, err := azidentity.NewDefaultAzureCredential(nil) credential, err := azidentity.NewDefaultAzureCredential(nil)
...@@ -80,24 +81,47 @@ func TestObjectStorage() { ...@@ -80,24 +81,47 @@ func TestObjectStorage() {
log.Fatal("Invalid credentials with error: " + err.Error()) 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 // Create the container
containerName := fmt.Sprintf("quickstart-%s", randomString()) containerName := fmt.Sprintf("quickstart-%s", randomString())
fmt.Printf("Creating a container named %s\n", containerName) fmt.Printf("Creating a container named %s\n", containerName)
containerClient := serviceClient.NewContainerClient(containerName) containerClient := azureCreds.ServiceClient.NewContainerClient(containerName)
_, err = containerClient.Create(ctx, nil) _, err := containerClient.Create(ctx, nil)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Printf("Creating a dummy file to test the upload and download\n") blobClient, err := azblob.NewBlockBlobClient(azureCreds.Url+containerName+"/"+baseObjectStorageName, azureCreds.Credential, nil)
data := []byte("\nhello world this is a blob\n")
blobName := "quickstartblob" + "-" + randomString()
blobClient, err := azblob.NewBlockBlobClient(url+containerName+"/"+blobName, credential, nil)
if err != nil { if err != nil {
log.Fatal(err) 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 // Upload to data to blob storage
_, err = blobClient.UploadBufferToBlockBlob(ctx, data, azblob.HighLevelUploadToBlockBlobOption{}) _, err = blobClient.UploadBufferToBlockBlob(ctx, data, azblob.HighLevelUploadToBlockBlobOption{})
......
package types 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 // Message - MessageBody can be of any type but will be use here for Transaction, AckTransaction
type Message struct { type Message struct {
MessageType string `json:"messageType"` MessageType string `json:"messageType"`
...@@ -22,3 +27,15 @@ type AckTransaction struct { ...@@ -22,3 +27,15 @@ type AckTransaction struct {
} }
type Database []Transaction 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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment