取得儲存體帳戶金鑰

 

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 必要項。 指定用於這個要求的作業版本。 此標頭應該設定為 2009年-10-01 或更高版本。 如需有關版本設定標頭的詳細資訊,請參閱 服務管理版本控制

要求本文

無。

回應

回應包括 HTTP 狀態碼、一組回應標頭和回應主體。

狀態碼

成功的作業會傳回狀態碼 200 (OK)。 如需狀態碼的相關資訊,請參閱 服務管理狀態和錯誤碼

回應標頭

這項作業的回應包括下列標頭。 回應也可能包括其他標準 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、相關聯的管理憑證指紋、作業版本字串和儲存體帳戶名稱,並且將傳回的儲存體帳戶金鑰列印到主控台。 初始化 msVersion, ,subscriptionId, ,thumbprintserviceName 具有自己的值來執行範例程式碼的變數。

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>