Azure SDK Go – 容器

huangapple go评论85阅读模式
英文:

azue sdk go - containers

问题

我可以使用Azure的Go SDK列出容器或存储账户的参数吗?我想要列出存储容量等信息。

以下是我的代码:

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,
			})

		}
	}
}

我想要列出这些容器的配置,主要是想要列出该账户的存储容量。我可以列出一些参数,但无法列出存储容量。

英文:

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:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

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

		accountKey := os.Getenv(&quot;ACCOUNTKEY&quot;)
		storageAccountName := os.Getenv(&quot;ACCOUNTNAME&quot;)
		//containerName := os.Getenv(&quot;CONTAINERNAME&quot;)

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

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

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

		}
	}
}

<!-- end snippet -->

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

得分: 1

您可以使用Azure Golang SDK中的GetProperties函数来获取Blob的属性和容量,示例如下:

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"

	// 创建凭据对象
	credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
	if err != nil {
		fmt.Println(err)
		return
	}

	// 创建管道对象
	p := azblob.NewPipeline(credential, azblob.PipelineOptions{})

	// 构建Blob URL
	u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net/%s/%s", accountName, containerName, blobName))
	blobURL := azblob.NewBlobURL(*u, p)

	// 获取Blob属性
	props, err := blobURL.GetProperties(context.Background(), azblob.BlobAccessConditions{}, azblob.ClientProvidedKeyOptions{})
	if err != nil {
		fmt.Println(err)
		return
	}

	// 打印Blob属性
	fmt.Printf("Blob名称:%s\n", blobName)
	fmt.Printf("Blob大小:%d\n", props.ContentLength())
	fmt.Printf("Blob类型:%s\n", props.ContentType())
	fmt.Printf("Blob最后修改时间:%s\n", props.LastModified())
}

命令:

go run storagequick.go

输出结果如下:

Azure SDK Go – 容器

Blob名称:blob.log
Blob大小:0
Blob类型:application/octet-stream
Blob最后修改时间:2023-04-19 13:59:54 +0000 GMT

第二段代码包括容器和Blob的属性:

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("创建凭据对象时出错:", err)
		os.Exit(1)
	}

	// 创建管道对象
	p := azblob.NewPipeline(credential, azblob.PipelineOptions{})

	// 构建服务URL
	u, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net", accountName))
	serviceURL := azblob.NewServiceURL(*u, p)

	// 创建容器URL
	containerURL := serviceURL.NewContainerURL(containerName)
	getContainerURL := containerURL.URL()

	fmt.Println("容器属性:")
	fmt.Println("")

	ctx := context.Background()

	// 获取容器属性
	containerProperties, err := containerURL.GetProperties(ctx, azblob.LeaseAccessConditions{})
	if err != nil {
		fmt.Println("获取容器属性时出错:", err)
		os.Exit(1)
	}

	fmt.Println("容器名称:", containerName)
	fmt.Println("容器最后修改时间:", containerProperties.LastModified())
	fmt.Println("")

	fmt.Println("Blob属性:")
	fmt.Println("")

	// 创建BlobURL
	blobURL := containerURL.NewBlobURL(blobName)
	getBlobURL := blobURL.URL()

	// 获取Blob属性
	blobProperties, err := blobURL.GetProperties(ctx, azblob.BlobAccessConditions{}, azblob.ClientProvidedKeyOptions{})
	if err != nil {
		fmt.Println("获取Blob属性时出错:", err)
		os.Exit(1)
	}

	fmt.Println("Blob名称:", blobName)
	fmt.Println("Blob大小:", blobProperties.ContentLength())
	fmt.Println("Blob类型:", blobProperties.ContentType())
	fmt.Println("Blob最后修改时间:", blobProperties.LastModified().Format(time.RFC3339))
	fmt.Println("")

	fmt.Println("容器URL:", getContainerURL)
	fmt.Println("Blob URL:", getBlobURL)
}

输出结果如下:

Azure SDK Go – 容器

Blob属性:

Blob名称:blob.log
Blob大小:0
Blob类型:application/octet-stream
Blob最后修改时间:2023-04-19T13:59:54Z

容器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 }
英文:

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

package main

  

import (

&quot;context&quot;

&quot;fmt&quot;

&quot;net/url&quot;

  

&quot;github.com/Azure/azure-storage-blob-go/azblob&quot;

)

  

func  main() {

accountName := &quot;strgaccountname&quot;

accountKey := &quot;&lt;account-key&gt;&quot;

containerName := &quot;silico-container&quot;

blobName := &quot;blob.log&quot;

  

// 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(&quot;https://%s.blob.core.windows.net/%s/%s&quot;, 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(&quot;Blob name: %s\n&quot;, blobName)

fmt.Printf(&quot;Blob size: %d\n&quot;, props.ContentLength())

fmt.Printf(&quot;Blob type: %s\n&quot;, props.ContentType())

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

}

Command :-

go run storagequick.go

Output:- Got blob properties like below:-

Azure SDK Go – 容器

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 (

&quot;context&quot;

&quot;fmt&quot;

&quot;net/url&quot;

&quot;os&quot;

&quot;time&quot;

  

&quot;github.com/Azure/azure-storage-blob-go/azblob&quot;

)

  

func  main() {

accountName := &quot;strgaccountname&quot;

accountKey := &quot;&lt;account-key&gt;&quot;

containerName := &quot;silico-container&quot;

blobName := &quot;blob.log&quot;

  

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

if err != nil {

fmt.Println(&quot;Error creating credential object:&quot;, err)

os.Exit(1)

}

  

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

  

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

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

  

containerURL := serviceURL.NewContainerURL(containerName)

getContainerURL := containerURL.URL()

  

fmt.Println(&quot;Container Properties:&quot;)

fmt.Println(&quot;&quot;)

  

ctx := context.Background()

  

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

if err != nil {

fmt.Println(&quot;Error getting container properties:&quot;, err)

os.Exit(1)

}

  

fmt.Println(&quot;Container name:&quot;, containerName)

fmt.Println(&quot;Container last modified:&quot;, containerProperties.LastModified())

fmt.Println(&quot;&quot;)

  

fmt.Println(&quot;Blob Properties:&quot;)

fmt.Println(&quot;&quot;)

  

blobURL := containerURL.NewBlobURL(blobName)

getBlobURL := blobURL.URL()

  

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

if err != nil {

fmt.Println(&quot;Error getting blob properties:&quot;, err)

os.Exit(1)

}

  

fmt.Println(&quot;Blob name:&quot;, blobName)

fmt.Println(&quot;Blob size:&quot;, blobProperties.ContentLength())

fmt.Println(&quot;Blob type:&quot;, blobProperties.ContentType())

fmt.Println(&quot;Blob last modified:&quot;, blobProperties.LastModified().Format(time.RFC3339))

fmt.Println(&quot;&quot;)

  

fmt.Println(&quot;Container URL:&quot;, getContainerURL)

fmt.Println(&quot;Blob URL:&quot;, getBlobURL)

Output:-

Azure SDK Go – 容器

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&gt; 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   }

huangapple
  • 本文由 发表于 2023年4月19日 20:41:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76054641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定