Exportera (0) Skriv ut
Visa allt
EN
Det här innehållet finns inte tillgängligt på ditt språk men här finns den engelska versionen,

Get Storage Account Keys

Updated: April 3, 2014

The Get Storage Keys operation returns the primary and secondary access keys for the specified storage account.

The Get Storage Keys request may be specified as follows. Replace <subscription-id> with your subscription ID, and <service-name> with the name of the storage account.

 

Method Request URI

GET

https://management.core.windows.net/<subscription-id>/services/storageservices/<service-name>/keys

You must make sure that the request that is made to the management service is secure. For additional details, see Authenticating Service Management Requests.

The following table describes the request headers.

 

Request Header Description

x-ms-version

Required. Specifies the version of the operation to use for this request. This header should be set to 2009-10-01 or higher. For more information about versioning headers, see Service Management Versioning.

The response includes an HTTP status code, a set of response headers, and a response body.

A successful operation returns status code 200 (OK). For information about status codes, see Service Management Status and Error Codes.

The response for this operation includes the following headers. The response may also include additional standard HTTP headers. All standard headers conform to the HTTP/1.1 protocol specification.

 

Response Header Description

x-ms-request-id

A value that uniquely identifies a request made against the management service.

The format of the response body is as follows:


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

The following table describes the elements of the response body.

 

Element name Description

Url

The Service Management API request URI used to perform Get Storage Account Properties requests against the storage account.

Primary

The primary access key for the storage account.

Secondary

The secondary access key for the storage account.

Use the storage keys returned from the Get Storage Account Keys operation to access the blob, queue, and table service endpoints for the storage.

The following example program takes the subscription ID, an associated management certificate thumbprint, the operation version string, and a storage account name, and prints the returned storage account keys to the console. Initialize the msVersion, subscriptionId, thumbprint and serviceName variables with your own values to run the example code.

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 = "http://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;
    }
}

When run, the program output will look something like the following sample:

Storage Account Keys for myexamplestorage1:
<StorageService xmlns="http://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>

Visa:
© 2014 Microsoft