WindowsAzure 之 Storage
1. 建立Storage account
a. 使用Windows Azure 帳號(LiveID)登陸 https://windows.azure.com
b. 點擊 瀏覽節點 ”Hosted Service, Storage Accounts & CDN”
c. 點擊二級瀏覽節點 “Storage Account”
d. 點擊 “NewStorage Account”
系統將顯示正在建立,大概需要10分鐘左右才能ready:
建立成功後如下:
e. 查看access key(存取金鑰), 點擊view 按鈕:
注意以上的密鑰是訪問Windows Azure Storage空間的唯一認證標識,切記不要泄漏,下面的實驗中我將會示範如何使用以上密鑰。
Windows Azure storage 三種主要的資料存放區方式:Bolb 隱藏檔,圖片等資料,Table儲存 本結構化的資料, Queue 儲存一些需要和其他的應用程式 交換資訊,存在Queue中的資料預設七天內會清除。
2. 在程式中配置storage 串連資訊
a. 在VS2010選中WebRole節點, 右擊選擇properties(屬性)
b. 點擊Settings(設定) 選項卡
c. 點擊”Add Setting”, 將Name改為StorageAccount,將Type改為 “Connection String”, 並點擊改行的 ”…”
d. 填寫Storage Account資訊,選擇Enter storageaccount credential , 填寫Account Name, Account Key(Primary accesskey 或 Second access key), 選擇 Use defaultHTTP endpoints, 點OK完成設定
3. 第一個訪問Storage 的程式(功能為建立一個名為pic的Bolb容器,容器可以理解為檔案夾)
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Microsoft.WindowsAzure;using Microsoft.WindowsAzure.ServiceRuntime;using Microsoft.WindowsAzure.StorageClient;using System.Threading;namespace WebRole1{ public partial class TestStroage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { this.TestBlob(); } } public void TestBlob() { CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => { configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)); RoleEnvironment.Changed += (anotherSender, arg) => { if (arg.Changes.OfType<RoleEnvironmentConfigurationSettingChange>() .Any((change) => (change.ConfigurationSettingName == configName))) { if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName))) { RoleEnvironment.RequestRecycle(); } } }; }); var storageAccount = CloudStorageAccount.FromConfigurationSetting("StroageAccount"); CloudBlobClient blobClient = new CloudBlobClient(storageAccount.BlobEndpoint, storageAccount.Credentials); CloudBlobContainer blobContainer = blobClient.GetContainerReference("pics"); BlobRequestOptions requestOption = new BlobRequestOptions(); IAsyncResult result = blobContainer.BeginCreateIfNotExist(requestOption, CreateContainerIfNotExistsCallback, blobContainer); while (!result.IsCompleted) { Thread.Sleep(1000); } } public void CreateContainerIfNotExistsCallback(IAsyncResult result) { CloudBlobContainer container = (CloudBlobContainer)result.AsyncState; //End the operation and indicate whether the container was created. if (container.EndCreateIfNotExist(result)) { this.Response.Write("Container created successfully."); } else { this.Response.Write("Container was not created because a container with that name already exists."); } } }}
以後的博文中本人會逐一舉例說明Bolb,Table , Queue的用法。
注: 如何建立並部署雲應用程式,請參考http://blog.csdn.net/farawayplace613/article/details/6933915