次の方法で共有


Regenerate Storage Account Keys

 

Regenerate Keys 非同期操作は、指定されたストレージ アカウントのプライマリまたはセカンダリ アクセス キーを再生成します。

要求

Regenerate Keys 要求は次のように指定することがあります。 置き換える <subscription-id> をサブスクリプション ID に、および <service-name> を目的のストレージ アカウントの名前。

メソッド 要求 URI
POST https://management.core.windows.net/<subscription-id>/services/storageservices/<service-name>/keys?action=regenerate

管理サービスに対して行われる要求をセキュリティで保護する必要があります。 詳細については、次を参照してください。 サービス管理要求の認証です。

URI パラメーター

URI パラメーター 説明
action=regenerate 必須です。 指定されたストレージ アカウントのプライマリ アクセス キーまたはセカンダリ アクセス キーの再生成が必要であることを指定します。

要求ヘッダー

要求ヘッダーの説明を次の表に示します。

要求ヘッダー 説明
Content-Type 必須です。 このヘッダーを設定 application/xmlです。
x-ms-version 必須です。 この要求に使用する操作のバージョンを指定します。 このヘッダーを設定する必要があります 2009年-10-01 またはそれ以降。 バージョン ヘッダーの詳細については、次を参照してください。 サービス管理のバージョン管理です。

要求本文

要求本文では、再生成するキー、プライマリ キーまたはセカンダリ キーが示されます。 要求本文の形式は次のとおりです。

  
<?xml version="1.0" encoding="utf-8"?> <RegenerateKeys xmlns="https://schemas.microsoft.com/windowsazure"> <KeyType>Primary|Secondary</KeyType> </RegenerateKeys>  
  

要求本文の要素を次の表に示します。

要素名 説明
KeyType 再生成するキーを指定します。 有効な値:

- プライマリ
- セカンダリ

応答

応答には、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-request-uri</Url> <StorageServiceKeys> <Primary>primary-key</Primary> <Secondary>secondary-key</Secondary> </StorageServiceKeys> </StorageService>  
  

応答本文の要素は次のとおりです。

要素名 説明
Url URI は、実行に使用されるサービス管理 API 要求 Get Storage Account Properties 、ストレージ アカウントに対する要求です。
プライマリ 指定されたストレージ アカウントのプライマリ キー。
セカンダリ 指定されたストレージ アカウントのセカンダリ キー。

解説

Regenerate Storage Account Keys 操作によって、再生成する、ストレージ アカウントのサービスのプライマリまたはセカンダリ キー。 によって返されるストレージ アカウントを使用してこの操作を使用できる、 ストレージ アカウントを一覧表示 操作は、ストレージ キーの更新の管理を自動化します。

使用例

次のサンプル プログラムでは、サブスクリプション ID、関連付けられている管理証明書のサムプリント、操作バージョン文字列、およびストレージ アカウント名を取得し、返されたストレージ アカウントのアクセス キーをコンソールに出力します。 呼び出して、 Regenerate Storage Account Keys をストレージ アカウント、および、新しいキー結果を出力のセカンダリ アクセス キーを再生成する操作。 初期化、 Version 定数をバージョン ヘッダー文字列では、 SubscriptionId 、サブスクリプションの GUID 識別子を持つ Thumbprint を管理証明書のサムプリント値でと ServiceName 例のコードを実行するには、ストレージ アカウントの名前。

namespace Microsoft.WindowsAzure.ServiceManagementRESTAPI.Samples { using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Security.Cryptography.X509Certificates; using System.Xml; using System.Xml.Linq; public class Program { // Set these constants with your values to run the sample. private const string Version = "2011-10-01"; private const string Thumbprint = "management-certificate-thumbprint"; private const string SubscriptionId = "subscription-id-guid"; private const string ServiceName = "storage-account-name"; // This is the common namespace for all Service Management REST API XML data. private static XNamespace wa = "https://schemas.microsoft.com/windowsazure"; /// <summary> /// Gets or sets the certificate that matches the Thumbprint value. /// </summary> private static X509Certificate2 Certificate { get; set; } static void Main(string[] args) { try { Certificate = GetCertificate(Thumbprint); XElement initialKeys = GetStorageAccountKeys(SubscriptionId, ServiceName); Console.WriteLine( "Storage Account Keys for {0}:{1}{2}", ServiceName, Environment.NewLine, initialKeys.ToString(SaveOptions.OmitDuplicateNamespaces)); string keyType = "Secondary"; XElement regeneratedKeys = RegenerateStorageAccountKeys(SubscriptionId, ServiceName, keyType); Console.WriteLine( "Regenerated {0} Storage Account Key for {1}:{2}{3}", keyType, ServiceName, Environment.NewLine, regeneratedKeys.ToString(SaveOptions.OmitDuplicateNamespaces)); } catch (Exception ex) { Console.WriteLine("Exception caught in Main:"); Console.WriteLine(ex.Message); } Console.Write("Press any key to continue:"); Console.ReadKey(); } /// <summary> /// Gets the certificate matching the thumbprint from the local store. /// Throws an ArgumentException if a matching certificate is not found. /// </summary> /// <param name="thumbprint">The thumbprint of the certificate to find.</param> /// <returns>The certificate with the specified thumbprint.</returns> private 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)); } /// <summary> /// Calls the Get Storage Account Keys operation in the Service Management /// REST API for the specified subscription and storage account name and returns /// the StorageService XML element from the response. /// </summary> /// <param name="subscriptionId">The subscription identifier.</param> /// <param name="serviceName">The name of the storage account.</param> /// <returns>The StorageService XML element from the response.</returns> private static XElement GetStorageAccountKeys( string subscriptionId, string serviceName) { string uriFormat = "https://management.core.windows.net/{0}" + "/services/storageservices/{1}/keys"; Uri uri = new Uri(String.Format(uriFormat, subscriptionId, serviceName)); XDocument responseBody; string requestId; HttpStatusCode statusCode = InvokeRequest(uri, "GET", null, out responseBody, out requestId); VerifyResult(uri, statusCode, HttpStatusCode.OK, responseBody); return responseBody.Element(wa + "StorageService"); } /// <summary> /// Calls the Regenerate Storage Account Keys operation in the Service /// Management REST API for the specified subscription, storage account name, /// and key, and returns the StorageService XML element from the response. /// </summary> /// <param name="subscriptionId">The subscription identifier.</param> /// <param name="serviceName">The name of the storage account.</param> /// <param name="keyType">A value of "Primary" or "Secondary" to specify the key to regenerate.</param> /// <returns>The StorageService XML element from the response.</returns> private static XElement RegenerateStorageAccountKeys( string subscriptionId, string serviceName, string keyType) { string uriFormat = "https://management.core.windows.net/{0}" + "/services/storageservices/{1}/keys?action=regenerate"; Uri uri = new Uri(String.Format(uriFormat, subscriptionId, serviceName)); XDocument requestBody = new XDocument( new XDeclaration("1.0", "UTF-8", "no"), new XElement( wa + "RegenerateKeys", new XElement(wa + "KeyType", keyType))); XDocument responseBody; string requestId; HttpStatusCode statusCode = InvokeRequest(uri, "POST", requestBody, out responseBody, out requestId); VerifyResult(uri, statusCode, HttpStatusCode.OK, responseBody); return responseBody.Element(wa + "StorageService"); } /// <summary> /// A helper function to invoke a Service Management REST API operation. /// </summary> /// <param name="uri">The URI of the operation to invoke using a web request.</param> /// <param name="method">The method of the web request, GET, PUT, POST, or DELETE.</param> /// <param name="requestBody">The XML body to send with the web request. Use null to send no request body.</param> /// <param name="responseBody">The XML body returned by the request, if any.</param> /// <param name="requestId">The requestId returned by the request, if any.</param> /// <returns>The status code returned by the request.</returns> private static HttpStatusCode InvokeRequest( Uri uri, string method, XDocument requestBody, out XDocument responseBody, out string requestId) { responseBody = null; requestId = String.Empty; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); request.Method = method; request.Headers.Add("x-ms-Version", Version); request.ClientCertificates.Add(Certificate); request.ContentType = "application/xml"; if (requestBody != null) { using (Stream requestStream = request.GetRequestStream()) { using (StreamWriter streamWriter = new StreamWriter( requestStream, System.Text.UTF8Encoding.UTF8)) { requestBody.Save(streamWriter, SaveOptions.DisableFormatting); } } } HttpWebResponse response; HttpStatusCode statusCode = HttpStatusCode.Unused; try { response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { // GetResponse throws a WebException for 4XX and 5XX status codes response = (HttpWebResponse)ex.Response; } try { statusCode = response.StatusCode; if (response.ContentLength > 0) { using (XmlReader reader = XmlReader.Create(response.GetResponseStream())) { responseBody = XDocument.Load(reader); } } if (response.Headers != null) { requestId = response.Headers["x-ms-request-id"]; } } finally { response.Close(); } return statusCode; } /// <summary> /// A helper function to throw an ApplicationException on unexpected /// status code responses from Service Management REST API operation calls. /// </summary> /// <param name="uri">The URI of the Service Management operation.</param> /// <param name="statusCode">The status code returned by the operation.</param> /// <param name="expectedCode">The expected status code.</param> /// <param name="responseBody">The response body returned by the operation.</param> private static void VerifyResult( Uri uri, HttpStatusCode statusCode, HttpStatusCode expectedCode, XDocument responseBody) { if (!statusCode.Equals(expectedCode)) { throw new ApplicationException(string.Format( "Call to {0} returned an error:{1}Status Code: {2} ({3}):{1}{4}", uri.ToString(), Environment.NewLine, (int)statusCode, statusCode, 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>XrmGWqu6qpgKX5G3lf+V5Bc0nFIGjGWiWhHTdMxkA5Mb4WjJ0rDV+3USWW/9fAWCrszrkr8+JUb1c5mxQdq4nw==</Primary> <Secondary>7jGHqUXHfASoUWeuClG9TXRZ59tTl5Fep8BNi5f86LFuewc0dQjDc6pyyju8BOi5947byrtgKnVtYO4f4cVdcg==</Secondary> </StorageServiceKeys> </StorageService> Regenerated Secondary Storage Account Key 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>XrmGWqu6qpgKX5G3lf+V5Bc0nFIGjGWiWhHTdMxkA5Mb4WjJ0rDV+3USWW/9fAWCrszrkr8+JUb1c5mxQdq4nw==</Primary> <Secondary>2Wd7lP3MesbkFi0LAltwUd40L6uu85N9VBtokOo2RfpqKA8q39orkJ6WUV3jP4TkZuYrSQNYxkQ5axcSzzTu8A==</Secondary> </StorageServiceKeys> </StorageService> Press any key to continue: