Windows Azure 用程式存取 BLOB Storage

加入 Windows Azure 雲端服務專案

在專案右鍵 > 加入 Windows Azure 雲端服務專案

幫現有的 ASP.NET MVC 4 加入 Window Azure 專案

image_thumb[14]

設定 Storage 連線

image

image

這邊的設定是本機 debug 的時候他會建立一個虛擬的 Storage 可以在開發的時候使用。

如果要使用本機的 Storage 的話使用

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));

或是使用

CloudStorageAccount storageAccount = CloudStorageAccount
.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

直接讀取 Config 設定

image

接下來在原本專案的 config 加入

  <connectionStrings>
<add name="StorageConnectionString"
connectionString="DefaultEndpointsProtocol=https;AccountName=[AccountName];AccountKey=[AccountKey]" />
</connectionStrings>

上傳 BLOB

如果 Container 設定是 public 只要上傳之後就可以直接從 Url 讀取了

        [AllowAnonymous]
public virtual ActionResult CreateImageUploadFile(HttpPostedFileBase imagePathFileUpload)
{
try
{
//檢查允許的附檔名
string[] allowExtensions = new string[] { "jpg", "jpeg", "gif", "png" };
WebImage picture = WebImage.GetImageFromRequest();
if (false == allowExtensions.Contains(picture.ImageFormat.ToLower()))
{
throw new ArgumentException("不支援上傳" + picture.FileName + "檔案格式不合");
}

string realFileName = Path.GetFileName(imagePathFileUpload.FileName);
//產生一組名稱
string uid = Regex.Replace(Guid.NewGuid().ToString(), "-", "").ToLower();
CloudStorageAccount storageAccount = CloudStorageAccount
.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("uploadsimages");
//如果 container 不存在就建立
container.CreateIfNotExist();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(uid + ".png");
//設定 contentType 並上傳
blockBlob.Properties.ContentType = "image/png";
blockBlob.UploadByteArray(picture.GetBytes());

return Json(new { msg = "上傳檔案成功", filename = blockBlob.Name, realFileName = realFileName }, "text/html");
}
catch (Exception ex)
{
return Json(new { msg = "上傳檔案失敗", error = ex.Message }, "text/html");
}
}

 

下載 BLOB

在某些時候或許不希望公開 container 而希望能夠透過一些驗證再讀取

/// <summary>
/// 顯示已經上傳的圖片
/// </summary>
/// <returns></returns>
[AllowAnonymous]
public ActionResult ShowImageUploadFile(string filename, int? width, int? height)
{
//可以在這邊判斷讀取權限
//利用連線字串取得連線
CloudStorageAccount storageAccount = CloudStorageAccount
.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//取得 Container
CloudBlobContainer container = blobClient.GetContainerReference("uploadsimages");
//取得 BLOB
CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
using (var fileStream = new MemoryStream())
{
blockBlob.DownloadToStream(fileStream);
WebImage image = new WebImage(fileStream);
return File(image.GetBytes(), blockBlob.Properties.ContentType);
}

}

Reference

http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/#header-4

http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/#header-5

http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/#header-6

http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/#header-7

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *