获取存储帐户密钥

 

Get Storage Keys 操作返回指定存储帐户的主访问密钥和辅助访问密钥。

请求

可以按以下方式指定 Get Storage Keys 请求。 将 <subscription-id> 替换为你的订阅 ID,将 <service-name> 替换为存储帐户的名称。

方法 请求 URI
GET https://management.core.windows.net/<subscription-id>/services/storageservices/<service-name>/keys

你必须确保向管理服务发出的请求是安全的。 有关其他详细信息,请参阅 进行身份验证服务管理请求

URI 参数

无。

请求标头

下表介绍请求标头。

请求标头 说明
x-ms-version 必需。 指定用于此请求的操作的版本。 应将此标头设置为 01.10.09 或更高版本。 有关版本控制标头的详细信息,请参阅 服务管理版本控制

请求正文

无。

响应

响应包括 HTTP 状态代码、一组响应标头和响应正文。

状态代码

此操作成功后返回状态代码 200(正常)。 有关状态代码的信息,请参阅 服务管理状态和错误代码

响应标头

此操作的响应包括以下标头。 该响应还可能包括其他标准 HTTP 标头。 所有标准标头都符合 HTTP/1.1 协议规范

响应标头 说明
x-ms-request-id 一个值,它唯一地标识对管理服务做出的请求。

响应正文

响应正文的格式如下所示:

  
<?xml version="1.0" encoding="utf-8"?> <StorageService xmlns="https://schemas.microsoft.com/windowsazure"> <Url>storage-service-url</Url> <StorageServiceKeys> <Primary>primary-key</Primary> <Secondary>secondary-key</Secondary> </StorageServiceKeys> </StorageService>  
  

下表介绍响应正文的元素。

元素名称 说明
Url 用于对存储帐户执行 获取存储帐户属性 请求的服务管理 API 请求 URI。
主要 存储帐户的主访问密钥。
辅助 存储帐户的辅助访问密钥。

备注

使用从 Get Storage Account Keys 操作返回的存储密钥访问存储的 blob、队列和表服务终结点。

示例

以下示例程序采用订阅 ID、关联的管理证书指纹、操作版本字符串和存储帐户名称,并将返回的存储帐户密钥打印到控制台。 用你自己的值初始化 msVersionsubscriptionIdthumbprintserviceName 变量以运行示例代码。

using System; using System.Collections.Generic; using System.Net; using System.Security.Cryptography.X509Certificates; using System.Xml; using System.Xml.Linq; class Program { static void Main(string[] args) { string msVersion = "2011-10-01"; string subscriptionId = "subscription-id-guid"; string thumbprint = "certificate-thumbprint"; string serviceName = "storage-service-name"; try { // Obtain the certificate with the specified thumbprint X509Certificate2 certificate = GetCertificate(thumbprint); GetStorageAccountKeysExample( subscriptionId, certificate, msVersion, serviceName); } catch (Exception ex) { Console.WriteLine("Exception caught in Main:"); Console.WriteLine(ex.Message); } } public static X509Certificate2 GetCertificate(string thumbprint) { List<StoreLocation> locations = new List<StoreLocation> { StoreLocation.CurrentUser, StoreLocation.LocalMachine }; foreach (var location in locations) { X509Store store = new X509Store("My", location); try { store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); X509Certificate2Collection certificates = store.Certificates.Find( X509FindType.FindByThumbprint, thumbprint, false); if (certificates.Count == 1) { return certificates[0]; } } finally { store.Close(); } } throw new ArgumentException(string.Format( "A certificate with thumbprint '{0}' could not be located.", thumbprint)); } public static void GetStorageAccountKeysExample( string subscriptionId, X509Certificate2 certificate, string version, string serviceName) { string uriFormat = "https://management.core.windows.net/{0}/services/storageservices/{1}/keys"; Uri uri = new Uri(String.Format(uriFormat, subscriptionId, serviceName)); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); request.Method = "GET"; request.Headers.Add("x-ms-version", version); request.ClientCertificates.Add(certificate); request.ContentType = "application/xml"; XDocument responseBody = null; HttpStatusCode statusCode; HttpWebResponse response; try { response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { // GetResponse throws a WebException for 400 and 500 status codes response = (HttpWebResponse)ex.Response; } statusCode = response.StatusCode; if (response.ContentLength > 0) { using (XmlReader reader = XmlReader.Create(response.GetResponseStream())) { responseBody = XDocument.Load(reader); } } response.Close(); if (statusCode.Equals(HttpStatusCode.OK)) { XNamespace wa = "https://schemas.microsoft.com/windowsazure"; XElement storageService = responseBody.Element(wa + "StorageService"); Console.WriteLine( "Storage Account Keys for {0}:{1}{2}", serviceName, Environment.NewLine, storageService.ToString(SaveOptions.OmitDuplicateNamespaces)); } else { Console.WriteLine("Call to Get Storage Account Keys returned an error:"); Console.WriteLine("Status Code: {0} ({1}):{2}{3}", (int)statusCode, statusCode, Environment.NewLine, responseBody.ToString(SaveOptions.OmitDuplicateNamespaces)); } return; } }  
  

运行后,程序输出将类似于以下示例:

Storage Account Keys for myexamplestorage1: <StorageService xmlns="https://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <Url>https://management.core.windows.net/01234567-89ab-cdef-0123-456789abcdef/services/storageservices/myexamplestorage1</Url> <StorageServiceKeys> <Primary>XrmGWqu9qpgKX5G3lf+V5Bc0nFIGjGWiWhHTdMxkA5Mb4WjJ0rDV+3USWW/6fAWCrszrkr2+JUb1c5mxQdq4nw==</Primary> <Secondary>VuXywhZaNbkh//SN70yL1w6na2H1FUOvjukSOAReQ6QM4kHNY7LmQUhgENw6Tp/SBz4y65R3Y5L5c5+zqXNvVA==</Secondary> </StorageServiceKeys> </StorageService>