英文:
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("./.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,
})
}
}
}
<!-- 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
输出结果如下:
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)
}
输出结果如下:
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 (
"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:-
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:-
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 }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论