Windows Azure Platform (二十二) Windows Azure Storage Service儲存服務之Blob詳解(上)Windows Azure Platform 系列文章目

來源:互聯網
上載者:User

《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
 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.