using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
namespace WebRole1
{
public partial class _Default : System.Web.UI.Page
{
public static CloudStorageAccount CSA // get CloudStorageAccount
{
get;
set;
}
private static CloudBlobClient cloudBlobClient = null;
public static CloudBlobClient CBC // get CloudBlobClient
{
get
{
if (cloudBlobClient != null) return cloudBlobClient;
cloudBlobClient = CSA.CreateCloudBlobClient();
return cloudBlobClient;
}
}
protected void Page_Load(object sender, EventArgs e)
{
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
{
configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
RoleEnvironment.Changed += (mysender, arg) =>
{
if (arg.Changes.OfType<RoleEnvironmentConfigurationSettingChange>()
.Any((change) => (change.ConfigurationSettingName == configName)))
{
if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))
{
RoleEnvironment.RequestRecycle();
}
}
};
});
CSA = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
CloudBlobContainer cont = CBC.GetContainerReference("testcontainer001");
cont.CreateIfNotExist();
// 正常ケースのテスト。アップロードしたアイテムはあるはず
CloudBlob blob1 = cont.GetBlobReference("test001");
blob1.UploadText("Hello!\r\n");
string result1 = blob1.DownloadText();
// 見つからないケース
string result2;
try
{
CloudBlob blob2 = cont.GetBlobReference("test002");
blob2.FetchAttributes();
result2 = "Succeded!\r\n";
}
catch (StorageClientException ex)
{
//if( ex.ErrorCode == StorageErrorCode.BlobNotFound ) //←これではダメ
if (ex.ErrorCode == StorageErrorCode.ResourceNotFound) //←これなら動いた
{
result2 = "not found\r\n"; // 見つからないケース
}
else
{
result2 = ex.ToString() + "\r\n"; // その他のエラーになったケース
}
}
Label1.Text = string.Format("{0} {1}", result1, result2);
}
}
}