匯出 (0) 列印
全部展開

更新儲存體帳戶

更新日期: 2014年4月

Update Storage Account 作業會針對 Windows Azure 中的儲存體帳戶更新標籤、更新描述,以及啟用或停用地理複寫狀態。

請使用下列格式指定 Update Storage Account 要求。請以您的訂用帳戶 ID 取代 <subscription-id>,並以儲存體帳戶的名稱取代 <service-name>

 

方法 要求 URI HTTP 版本

PUT

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

HTTP/1.1

無。

下表描述必要的和選用的要求標頭。

 

要求標頭 說明

Content-Type

必要項。請將此標頭設定為 application/xml

x-ms-version

必要項。指定用於這個要求的作業版本。此標頭的值必須設定為 2011-06-01 或更新版本。目前的版本是 2012-03-01。如需有關版本設定標頭的詳細資訊,請參閱<服務管理版本設定>。

要求主體的格式如下:


<?xml version="1.0" encoding="utf-8"?>
<UpdateStorageServiceInput xmlns="http://schemas.microsoft.com/windowsazure">
  <Description>description-of-storage-account</Description>
  <Label>base64-encoded-label</Label>
  <GeoReplicationEnabled>geo-replication-indicator</GeoReplicationEnabled>
  <ExtendedProperties>
    <ExtendedProperty>
      <Name>property-name</Name>
      <Value>property-value</Value>
    </ExtendedProperty>
  </ExtendedProperties>
  <CustomDomains>
    <CustomDomain>
      <Name>name-of-custom-domain</Name>
      <UseSubDomainName>cname-validation-indicator</UseSubDomainName>
    </CustomDomain>
  </CustomDomains>
  <SecondaryReadEnabled>secondary-read-indicator</SecondaryReadEnabled>
</UpdateStorageServiceInput>

Important重要事項
要求主體中元素的順序十分重要。如果需要某個元素,它在 XML 中出現的順序必須如上所示。

要求主體的格式如下。

 

元素名稱

說明

Label

(選擇性)。指定 base-64 編碼格式的儲存體帳戶名稱。標籤的長度最多可以有 100 個字元。在您進行追蹤時,此標籤可用來識別儲存體帳戶。您必須針對 LabelDescription 或這兩者指定值。

Description

選擇項。儲存體帳戶的描述。描述的長度最多可以有 1024 個字元。您必須針對 LabelDescription 或這兩者指定值。

GeoReplicationEnabled

選擇項。啟用或停用指定之儲存體的地理複寫。如果設定為 true,儲存體帳戶中的資料就會在多個地理位置之間複寫,以便在面臨重大服務中斷的情況時啟用恢復功能。如果設定為 false,就會停用地理複寫。若未在要求主體中包含此元素,則目前的值就會保持不變。

Important重要事項
如果您已啟用地理複寫,可以將此元素設定為 false,藉以選擇停用地理複寫。停用之後,您的資料就不會再複寫至次要資料中心,而且系統會移除次要位置的任何資料。

在停用之後啟用地理複寫會導致系統向儲存體帳戶收取將目前資料複本複寫至次要資料中心的費用。當現有的資料複本複寫至次要資料中心之後,更新的地理複寫完全免費。

必須使用 2012-03-01 版或更高版本,才能使用 GeoReplicationEnabled 元素。如果在 2013-11-01 版本之後指定了 SecondaryReadEnabled,也應該要指定 GeoReplicationEnabled

ExtendedProperties

指定已加入至儲存體帳戶之擴充屬性的名稱和值。

CustomDomains

指定與儲存體帳戶相關聯的自訂網域。

CustomDomains 元素僅適用於 2013-06-01 版本或更高版本。

SecondaryReadEnabled

指出儲存體帳戶已啟用次要讀取。

可能的值為:

  • true

  • false

必須使用 2013-11-01 版或更高版本,才能使用 SecondaryReadEnabled 元素。

指定已加入至儲存體帳戶之擴充屬性的名稱和值。

 

元素名稱 說明

Name

(選擇性)。代表擴充儲存體帳戶屬性的名稱。每個擴充屬性都必須具有已定義的名稱和值。您最多可以擁有 50 個擴充屬性名稱/值組。

Name 元素的長度上限為 64 個字元、只有英數字元和底線可用於 Name,而且名稱必須以字母當做開頭。如果您嘗試使用其他字元、以非字母字元當做 Name 的開頭,或者輸入的名稱與相同儲存體帳戶所擁有之其他擴充屬性的名稱完全相同,就會產生狀態碼 400 (不正確的要求) 錯誤。

必須使用 2012-03-01 版或更高版本,才能使用 Name 元素。

Value

(選擇性) 代表擴充儲存體帳戶屬性的值。每個擴充屬性都必須具有已定義的名稱和值。您最多可以擁有 50 個擴充屬性名稱/值組,而且每個擴充屬性值的長度上限為 255 個字元。

必須使用 2012-03-01 版或更高版本,才能使用 Value 元素。

指定與儲存體帳戶相關聯的自訂網域。

 

元素名稱 說明

CustomDomain

指定與儲存體帳戶相關聯之自訂網域的相關資訊。

Name

指定自訂網域的名稱。

UseSubDomainName

指出是否啟用 CName 驗證。

可能的值為:

  • true

  • false

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

成功的作業會傳回狀態碼 200 (確定)。

如需狀態碼的資訊,請參閱<服務管理狀態和錯誤碼>。

這項作業的回應包括下列標頭。回應也可能包括其他標準 HTTP 標頭。所有標準標頭都符合 HTTP/1.1 通訊協定規格

 

回應標頭 說明

x-ms-request-id

唯一識別對管理服務發出之要求的值。若為非同步作業,您可以使用標頭的值來呼叫 Get Operation Status,以便判斷作業已完成、失敗,還是仍在進行中。如需詳細資訊,請參閱<追蹤非同步服務管理要求>。

無。

<subscription-id> 指定之訂用帳戶相關聯的任何管理憑證都可用來驗證此作業。如需其他詳細資訊,請參閱<驗證服務管理要求>。

使用 Update Storage Account 作業可以變更儲存體帳戶的描述、標籤或地理複寫設定值。您可以使用 取得儲存體帳戶屬性 作業來檢閱這些值。

下列範例會呼叫 取得儲存體帳戶屬性 作業以列出儲存體帳戶的初始屬性值、透過 Update Storage Account 作業的呼叫來更新 LabelDescription 屬性,然後透過另一個 取得儲存體帳戶屬性 的呼叫來顯示更新的結果。請將 Version 字串的值變更為所需的版本、將 Thumbprint 值變更為管理憑證的憑證指紋、將 SubscriptionId 變更為您的訂用帳戶識別碼,並將 ServiceName 值變更為儲存體帳戶的名稱,以便執行此範例。

namespace Microsoft.WindowsAzure.ServiceManagementRESTAPI.Samples
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    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-12-01";
        private const string Thumbprint = "management-certificate-thumbprint";
        private const string SubscriptionId = "subscription-identifier";
        private const string ServiceName = "storage-account-name";

        // This is the common namespace for all Service Management REST API XML data.
        private static XNamespace wa = "http://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 = GetStoreCertificate(Thumbprint);

                // Get the initial property values for the storage account.
                // Convert the Label property to a readable value for display.
                XElement initialProperties =
                    GetStorageAccountProperties(ServiceName);
                XElement labelElement = initialProperties.Descendants(wa + "Label").First();
                labelElement.Value = labelElement.Value.FromBase64();
                Console.WriteLine(
                    "Storage Account Properties for {0}:{1}{2}",
                    ServiceName,
                    Environment.NewLine,
                    initialProperties.ToString(SaveOptions.OmitDuplicateNamespaces));

                // Update the label and description of the storage account.
                string label = String.Format("updated_{0}_label", ServiceName);
                string description = String.Format(
                    "Updated description for {0}",
                    ServiceName);
                UpdateStorageAccount(ServiceName, label, description, 
                    geoReplicationEnabled: false);

                // Get the updated property values for the storage account.
                // Convert the Label property to a readable value for display.
                XElement updatedProperties =
                    GetStorageAccountProperties(ServiceName);
                labelElement = updatedProperties.Descendants(wa + "Label").First();
                labelElement.Value = labelElement.Value.FromBase64();
                Console.WriteLine(
                    "Updated Storage Account Properties for {0}:{1}{2}",
                    ServiceName,
                    Environment.NewLine,
                    updatedProperties.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>
        /// Calls the Get Storage Account Properties 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="serviceName">The name of the storage account.</param>
        /// <returns>The StorageService XML element from the response.</returns>
        private static XElement GetStorageAccountProperties(
            string serviceName)
        {
            string uriFormat = "https://management.core.windows.net/{0}" +
                "/services/storageservices/{1}";
            Uri uri = new Uri(String.Format(uriFormat, SubscriptionId, serviceName));
            XDocument responseBody;
            InvokeRequest(uri, "GET", HttpStatusCode.OK, null, out responseBody);
            return responseBody.Element(wa + "StorageService");
        }

        /// <summary>
        /// Calls the Update Storage Account operation in the Service Management 
        /// REST API for the specified subscription, storage account name, new 
        /// description, label, and geo-replication enabled setting.
        /// </summary>
        /// <param name="serviceName">The name of the storage account to update.</param>
        /// <param name="label">The new label for the storage account.</param>
        /// <param name="description">The new description for the storage account.</param>
        /// <param name="geoReplicationEnabled">The new geo-replication setting, if applicable. 
        /// This optional parameter defaults to null.</param>
        private static void UpdateStorageAccount(
            string serviceName,
            string label,
            string description,
            bool? geoReplicationEnabled = null)
        {
            string uriFormat = "https://management.core.windows.net/{0}" +
                "/services/storageservices/{1}";
            Uri uri = new Uri(String.Format(uriFormat, SubscriptionId, serviceName));
            XDocument requestBody = new XDocument(
                new XDeclaration("1.0", "UTF-8", "no"),
                new XElement(
                    wa + "UpdateStorageServiceInput",
                    new XElement(wa + "Description", description),
                    new XElement(wa + "Label", label.ToBase64())));

            // Add the GeoReplicationEnabled element if the version supports it.
            if ((geoReplicationEnabled != null) &&
                (String.CompareOrdinal(Version, "2011-12-01") >= 0))
            {
                requestBody.Element(wa + "UpdateStorageServiceInput").Add(
                    new XElement(
                        wa + "GeoReplicationEnabled", 
                        geoReplicationEnabled.ToString().ToLowerInvariant()));
            }

            XDocument responseBody;
            InvokeRequest(uri, "PUT", HttpStatusCode.OK, requestBody, out responseBody);
        }

        /// <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 GetStoreCertificate(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>
        /// A helper function to invoke a Service Management REST API operation.
        /// Throws an ApplicationException on unexpected status code results.
        /// </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="expectedCode">The expected status code.</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>
        /// <returns>The requestId returned by the operation.</returns>
        private static string InvokeRequest(
            Uri uri,
            string method,
            HttpStatusCode expectedCode,
            XDocument requestBody,
            out XDocument responseBody)
        {
            responseBody = null;
            string 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();
            }

            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 requestId;
        }
    }

    /// <summary>
    /// Helpful extension methods for converting strings to and from Base-64.
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// Converts a UTF-8 string to a Base-64 version of the string.
        /// </summary>
        /// <param name="s">The string to convert to Base-64.</param>
        /// <returns>The Base-64 converted string.</returns>
        public static string ToBase64(this string s)
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
            return Convert.ToBase64String(bytes);
        }

        /// <summary>
        /// Converts a Base-64 encoded string to UTF-8.
        /// </summary>
        /// <param name="s">The string to convert from Base-64.</param>
        /// <returns>The converted UTF-8 string.</returns>
        public static string FromBase64(this string s)
        {
            byte[] bytes = Convert.FromBase64String(s);
            return System.Text.Encoding.UTF8.GetString(bytes);
        }
    }
}

執行時,範例程式將會產生類似下列範例的輸出:

Storage Account Properties 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>
  <ServiceName>myexamplestorage1</ServiceName>
  <StorageServiceProperties>
    <Description>myexamplestorage1 description</Description>
    <Location>North Central US</Location>
    <Label>My Example Label</Label>
    <Status>Created</Status>
    <Endpoints>
      <Endpoint>http://myexamplestorage1.blob.core.windows.net/</Endpoint>
      <Endpoint>http://myexamplestorage1.queue.core.windows.net/</Endpoint>
      <Endpoint>http://myexamplestorage1.table.core.windows.net/</Endpoint>
    </Endpoints>
    <GeoReplicationEnabled>true</GeoReplicationEnabled>
    <GeoPrimaryRegion>usnorth</GeoPrimaryRegion>
    <StatusOfPrimary>Available</StatusOfPrimary>
    <GeoSecondaryRegion>ussouth</GeoSecondaryRegion>
    <StatusOfSecondary>Available</StatusOfSecondary>
  </StorageServiceProperties>
</StorageService>
Updated Storage Account Properties 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>
  <ServiceName>myexamplestorage1</ServiceName>
  <StorageServiceProperties>
    <Description>Updated description for myexamplestorage1</Description>
    <Location>North Central US</Location>
    <Label>updated_myexamplestorage1_label</Label>
    <Status>Created</Status>
    <Endpoints>
      <Endpoint>http://myexamplestorage1.blob.core.windows.net/</Endpoint>
      <Endpoint>http://myexamplestorage1.queue.core.windows.net/</Endpoint>
      <Endpoint>http://myexamplestorage1.table.core.windows.net/</Endpoint>
    </Endpoints>
    <GeoReplicationEnabled>false</GeoReplicationEnabled>
  </StorageServiceProperties>
</StorageService>
Press any key to continue:

另請參閱

顯示:
© 2014 Microsoft