azue sdk go - containers

146 Views Asked by At

can i list the parameters of my container or my storage account using azure for go sdk?

i want to list something like storage capacity etc

this is my code:

func GetContainer() gin.HandlerFunc {
    return func(c *gin.Context) {
        err := godotenv.Load("./.env")
        if err != nil {
            log.Println("Erro ao carregar variavel de ambiente", err)
            c.JSON(http.StatusInternalServerError, gin.H{
                "Erro ao carregar variavel de ambiente": err,
            })
            return
        }

        accountKey := os.Getenv("ACCOUNTKEY")
        storageAccountName := os.Getenv("ACCOUNTNAME")
        //containerName := os.Getenv("CONTAINERNAME")

        client, err := storage.NewBasicClient(storageAccountName, accountKey)
        if err != nil {
            c.JSON(http.StatusInternalServerError, gin.H{
                "Erro ao criar cliente: ": err,
            })
            log.Fatal("Erro ao criar cliente: ", err)
            return
        }

        containerSvc := client.GetBlobService()
        containerListResponse, err := containerSvc.ListContainers(storage.ListContainersParameters{})
        if err != nil {
            c.JSON(http.StatusInternalServerError, gin.H{
                "Erro ao listar containers: ": err,
            })
            log.Fatal("Erro ao listar containers: ", err)
            return
        }

        for _, container := range containerListResponse.Containers {
            c.JSON(http.StatusOK, gin.H{
                "Containers":   container.Name,
                "Propriedades": container.Properties,
            })

        }
    }
}

list the configurations of these containers that I'm going to list, what I mainly want is to list the storage capacity of this account

I can list some parameters, but I can't list the storage capacity

1

There are 1 best solutions below

1
On

You can use GetProperties function in your azure golang sdk to get the blob properties and its capacity like below:-

package main

  

import (

"context"

"fmt"

"net/url"

  

"github.com/Azure/azure-storage-blob-go/azblob"

)

  

func  main() {

accountName := "strgaccountname"

accountKey := "<account-key>"

containerName := "silico-container"

blobName := "blob.log"

  

// Create a credential object from your account key.

credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)

if err != nil {

fmt.Println(err)

return

}

  

// Create a pipeline object with default pipeline options.

p := azblob.NewPipeline(credential, azblob.PipelineOptions{})

  

// Construct the blob URL.

u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net/%s/%s", accountName, containerName, blobName))

blobURL := azblob.NewBlobURL(*u, p)

  

// Get the blob properties.

props, err := blobURL.GetProperties(context.Background(), azblob.BlobAccessConditions{}, azblob.ClientProvidedKeyOptions{})

if err != nil {

fmt.Println(err)

return

}

  

// Print the blob properties.

fmt.Printf("Blob name: %s\n", blobName)

fmt.Printf("Blob size: %d\n", props.ContentLength())

fmt.Printf("Blob type: %s\n", props.ContentType())

fmt.Printf("Blob last modified: %s\n", props.LastModified())

}

Command :-

go run storagequick.go

Output:- Got blob properties like below:-

enter image description here

Blob name: blob.log
Blob size: 0
Blob type: application/octet-stream
Blob last modified: 2023-04-19 13:59:54 +0000 GMT

Code 2 with container and blob properties:-

package main

  

import (

"context"

"fmt"

"net/url"

"os"

"time"

  

"github.com/Azure/azure-storage-blob-go/azblob"

)

  

func  main() {

accountName := "strgaccountname"

accountKey := "<account-key>"

containerName := "silico-container"

blobName := "blob.log"

  

credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)

if err != nil {

fmt.Println("Error creating credential object:", err)

os.Exit(1)

}

  

p := azblob.NewPipeline(credential, azblob.PipelineOptions{})

  

u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", accountName))

serviceURL := azblob.NewServiceURL(*u, p)

  

containerURL := serviceURL.NewContainerURL(containerName)

getContainerURL := containerURL.URL()

  

fmt.Println("Container Properties:")

fmt.Println("")

  

ctx := context.Background()

  

containerProperties, err := containerURL.GetProperties(ctx, azblob.LeaseAccessConditions{})

if err != nil {

fmt.Println("Error getting container properties:", err)

os.Exit(1)

}

  

fmt.Println("Container name:", containerName)

fmt.Println("Container last modified:", containerProperties.LastModified())

fmt.Println("")

  

fmt.Println("Blob Properties:")

fmt.Println("")

  

blobURL := containerURL.NewBlobURL(blobName)

getBlobURL := blobURL.URL()

  

blobProperties, err := blobURL.GetProperties(ctx, azblob.BlobAccessConditions{}, azblob.ClientProvidedKeyOptions{})

if err != nil {

fmt.Println("Error getting blob properties:", err)

os.Exit(1)

}

  

fmt.Println("Blob name:", blobName)

fmt.Println("Blob size:", blobProperties.ContentLength())

fmt.Println("Blob type:", blobProperties.ContentType())

fmt.Println("Blob last modified:", blobProperties.LastModified().Format(time.RFC3339))

fmt.Println("")

  

fmt.Println("Container URL:", getContainerURL)

fmt.Println("Blob URL:", getBlobURL)

Output:-

enter image description here

Blob Properties:

Blob name: blob.log
Blob size: 0
Blob type: application/octet-stream
Blob last modified: 2023-04-19T13:59:54Z

Container URL: {https   siliconstrg43.blob.core.windows.net /silico-container  false false   }
Blob URL: {https   siliconstrg43.blob.core.windows.net /silico-container/blob.log  false false   }
PS C:\gostrg\storage-blobs-go-quickstart> go run storagecontainer.go
Container Properties:

Container name: silico-container
Container last modified: 2023-04-19 13:59:06 +0000 GMT

Blob Properties:

Blob name: blob.log
Blob size: 0
Blob type: application/octet-stream
Blob last modified: 2023-04-19T13:59:54Z

Container URL: {https   siliconstrg43.blob.core.windows.net /silico-container  false false   }
Blob URL: {https   siliconstrg43.blob.core.windows.net /silico-container/blob.log  false false   }