Get Storage Account Keys

 

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 URI は、実行に使用されるサービス管理 API 要求 Get Storage Account Properties 、ストレージ アカウントに対する要求です。
プライマリ ストレージ アカウントのプライマリ アクセス キー。
セカンダリ ストレージ アカウントのセカンダリ アクセス キー。

解説

返されるストレージ キーを使用して、 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>