获取存储帐户密钥的Go客户端示例

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

Go client example to fetch storage account keys

问题

我们可以使用 Azure SDK for Go 来获取存储帐户密钥。以下是一个示例代码片段,展示了如何使用存储帐户名称和其他参数来获取存储帐户密钥:

package main

import (
	"context"
	"fmt"
	"github.com/Azure/azure-sdk-for-go/profiles/latest/storage/mgmt/storage"
	"github.com/Azure/go-autorest/autorest/azure/auth"
)

func main() {
	// 设置订阅 ID、资源组名称和存储帐户名称
	subscriptionID := "<your-subscription-id>"
	resourceGroupName := "<your-resource-group-name>"
	accountName := "<your-storage-account-name>"

	// 创建新的存储帐户客户端
	authorizer, err := auth.NewAuthorizerFromEnvironment()
	if err != nil {
		fmt.Println("Failed to create authorizer:", err)
		return
	}
	client := storage.NewAccountsClient(subscriptionID)
	client.Authorizer = authorizer

	// 获取存储帐户密钥
	keys, err := client.ListKeys(context.Background(), resourceGroupName, accountName)
	if err != nil {
		fmt.Println("Failed to list storage account keys:", err)
		return
	}

	// 打印第一个密钥
	if len(*keys.Keys) > 0 {
		fmt.Println("Storage account key:", (*keys.Keys)[0].Value)
	} else {
		fmt.Println("No storage account keys found.")
	}
}

请确保在运行代码之前,已经安装了 github.com/Azure/azure-sdk-for-gogithub.com/Azure/go-autorest/autorest/azure/auth 这两个包,并替换示例代码中的 <your-subscription-id><your-resource-group-name><your-storage-account-name> 为实际的订阅 ID、资源组名称和存储帐户名称。

希望这可以帮助到你!

英文:

How can we get the Azure storage account key from storage account name and other parameters ?

I tried to build the storage account client but it requires, storage account name and key to build the client. I want to fetch programatically storage account key using storage account name and other parameters. Equivalent Go Sample code to below Azure CLI command.

az storage account keys list --resource-group --account-name

Can you please give some pointer, how can i fetch using Go sample code ?

Thank you

答案1

得分: 2

要获取存储帐户的密钥,您需要使用Azure SDK for Go,特别是armstorage

以下是列出帐户密钥的代码示例:

func ExampleStorageAccountsClient_ListKeys() {
	cred, err := azidentity.NewDefaultAzureCredential(nil)
	if err != nil {
		log.Fatalf("无法获取凭据:%v", err)
	}
	client := armstorage.NewStorageAccountsClient(arm.NewDefaultConnection(cred, nil), "<订阅 ID>")
	resp, err := client.ListKeys(context.Background(), "<资源组名称>", "<存储帐户名称>", nil)
	if err != nil {
		log.Fatalf("无法删除帐户:%v", err)
	}
	for _, k := range resp.StorageAccountListKeysResult.Keys {
		log.Printf("帐户密钥:%v", *k.KeyName)
	}
}

此示例和更多代码示例可在此处找到:https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/storage/armstorage/example_storageaccounts_test.go。

英文:

To get keys for a storage account, you will need to make use of Azure SDK for Go especially armstorage`.

Here's the code sample for listing account keys:

func ExampleStorageAccountsClient_ListKeys() {
	cred, err := azidentity.NewDefaultAzureCredential(nil)
	if err != nil {
		log.Fatalf(&quot;failed to obtain a credential: %v&quot;, err)
	}
	client := armstorage.NewStorageAccountsClient(arm.NewDefaultConnection(cred, nil), &quot;&lt;subscription ID&gt;&quot;)
	resp, err := client.ListKeys(context.Background(), &quot;&lt;resource group name&gt;&quot;, &quot;&lt;storage account name&gt;&quot;, nil)
	if err != nil {
		log.Fatalf(&quot;failed to delete account: %v&quot;, err)
	}
	for _, k := range resp.StorageAccountListKeysResult.Keys {
		log.Printf(&quot;account key: %v&quot;, *k.KeyName)
	}
}

This sample and more code examples are available here: https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/storage/armstorage/example_storageaccounts_test.go.

huangapple
  • 本文由 发表于 2021年9月21日 12:06:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/69263253.html
匿名

发表评论

匿名网友

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

确定