《Windows Azure Platform 系列文章目錄》
本章我們會介紹如何在本地模擬器使用Blob Storage儲存圖片。
關於Blob Storage的概念,請參考 Windows Azure Platform (七) Windows Azure Storage Service儲存服務 。
在開始介紹之前,請確保您已經下載了最新的Windows Azure開發工具,我使用的是1.6版本。本次介紹使用Visual Studio 2010作為開發工具。
1.建立Azure Project
以管理員身份運行Visual Studio 2010,並建立一個Windows Azure Project,我命名為AzureBlobStorage
然後選擇WebRole-->右鍵-->屬性
在Settings頁面,Add Setting,類型選擇Connection String,並且按最右邊的"..."按鈕
選擇"Use the Windows Azure storage emulator",然後選擇"OK"
然後添加另外一個Setting(設定),叫做ContainerName並且把它的Type改成"String",值設定成gallery
注意:
Container和Blob的命名規則!
2.開啟Default.aspx.cs
添加引用命名空間
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using System.Collections.Specialized;
using Microsoft.WindowsAzure.StorageClient.Protocol;
前端的ASPX添加如下控制項:
然後添加代碼內容:
namespace MyWebRole
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.EnsureContainerExists();
}
}
private void EnsureContainerExists()
{
var container = GetContainer();
// 檢查container是否被建立,如果沒有,建立container
container.CreateIfNotExist();
var permissions = container.GetPermissions();
//對Storage的存取權限是可以瀏覽Container
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(permissions);
}
private CloudBlobContainer GetContainer()
{
//擷取ServiceConfiguration.cscfg設定檔的資訊
var account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
var client = account.CreateCloudBlobClient();
//獲得BlobContainer對象
return client.GetContainerReference(RoleEnvironment.GetConfigurationSettingValue("ContainerName"));
}
protected void upload_Click(object sender, EventArgs e)
{
if (imageFile.HasFile)
{
//輸出圖片檔案的資訊
status.Text = "Inserted [" + imageFile.FileName + "] - Content Type [" + imageFile.PostedFile.ContentType + "] - Length [" + imageFile.PostedFile.ContentLength + "]";
this.SaveImage(imageFile.FileName,imageFile.PostedFile.ContentType,imageFile.FileBytes);
}
else
status.Text = "No image file";
}
private void SaveImage(string fileName, string contentType, byte[] data)
{
//獲得BlobContainer對象並把檔案上傳到這個Container
var blob = this.GetContainer().GetBlobReference(fileName);
blob.Properties.ContentType = contentType;
// 建立中繼資料資訊
var metadata = new NameValueCollection();
metadata["Name"] = fileName;
// 上傳圖片
blob.Metadata.Add(metadata);
blob.UploadByteArray(data);
}
}
3.開啟Global.asax.cs檔案,添加如下代碼
void Application_Start(object sender, EventArgs e)
{
// This code sets up a handler to update CloudStorageAccount instances when their corresponding
// configuration settings change in the service configuration file.
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
{
// Provide the configSetter with the initial value
configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
});
}
4.運行應用程式,使用FileUpload尋找本地圖片,然後點擊"Upload Image",圖片就能上傳到本地Storage Emulator的Blob裡了。
6.開啟Visual Studio,展開左側的Server Explorer列表,依次展開Windows Azure Storage-->Development-->Blob-->gallery
其中Developement表示我使用的是本地的模擬器
gallery就是我前面設定的Blob Container
我們選中Azure.jpg,右鍵屬性,可以看使用Storage Emluator後在本地的URL
我們可以通過開啟本地的瀏覽器來查看。
5.下載我的工程檔案:AzureBlobStorage.rar