英文:
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-go
和 github.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("failed to obtain a credential: %v", err)
}
client := armstorage.NewStorageAccountsClient(arm.NewDefaultConnection(cred, nil), "<subscription ID>")
resp, err := client.ListKeys(context.Background(), "<resource group name>", "<storage account name>", nil)
if err != nil {
log.Fatalf("failed to delete account: %v", err)
}
for _, k := range resp.StorageAccountListKeysResult.Keys {
log.Printf("account key: %v", *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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论