简体   繁体   English

以编程方式获取存储帐户密钥

[英]Programmatically Get Storage Account Key

I am building a UWP application.我正在构建一个 UWP 应用程序。 I want to upload and download data to Azure Blob containers belonging to various storage accounts.我想上传和下载数据到属于各种存储帐户的 Azure Blob 容器。 I do not intend to store the connection strings as they keep rotating.我不打算在它们不断旋转时存储连接字符串。

Is there a way to dynamically get the storage account key?有没有办法动态获取存储帐户密钥?

(I have the container name and Blob URL) (我有容器名称和 Blob URL)

Once I get the account key, I can generate the connection string using:获得帐户密钥后,我可以使用以下方法生成连接字符串:

DefaultEndpointsProtocol=https;AccountName=<AccountName>;AccountKey=<???>;EndpointSuffix=core.windows.net

Using Azure.Storage.Blob - V12使用 Azure.Storage.Blob - V12

Way-1: There is one way to get the Storage account key is by using Azure CLI commands as below and I followed Microsoft-Document :方式 1:有一种获取存储帐户密钥的方法是使用 Azure CLI 命令,如下所示,我遵循了 Microsoft 文档

az storage account keys list -g xx -n yy

xx- Name of the resource group yy- Name of Storage account xx- 资源组名称 yy- 存储帐户名称

在此处输入图像描述

Way 2: To get programmatically, Thanks to @Ivan Yang and I followed his SO-Thread :方式 2:要以编程方式获取,感谢@Ivan Yang,我关注了他的SO-Thread

 keys = storage_client.storage_accounts.list_keys(RESOURCE_GROUP_NAME,STORAGE_ACCOUNT_NAME)

So the Storage Account key is the value of keys.keys[0].value所以存储帐户密钥是keys.keys[0].value的值

Way 3: You can also get storage account key by using api and call the below api in c# to get the Key and I followed Microsoft-Document as below: POST Req:方式3:您也可以使用api获取存储帐户密钥,并在c#中调用以下api获取密钥,我按照以下Microsoft文档进行操作:POST Req:

https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}/listKeys?api-version=2021-09-01

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM