Windows Azure Drive を使ってみます。
Windows Azure Drive は、AzureからNTFSとしてアクセスするサービスです。ドライブレターを使ったマウントも可能です。詳しくは ホワイトペーパー(Windows Azure ドライブ) をご覧下さい。
環境
Visual Studio 2010
Windows Azure Tools for Microsoft Visual Studio 2010 1.1
準備
1.参照設定に Microsoft.WindowsAzure.CloudDrive.dll を追加します。
2.Local Storage に設定を追加します。
例)Name=LocalStorage1, Size=1000(MB)
3.ServiceConfiguration.cscfg の修正を行います。
特に1.2を使用する理由はないのですが、今回は GUEST OS のバージョンで1.2を指定しています。
<ServiceConfiguration serviceName="SampleCloud"
xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration"
osVersion="WA-GUEST-OS-1.2_201003-01">
コード
サンプルコードでは、コンテナがなかったらコンテナを作成し、オンディスク キャッシュを使ってマウントしています。
また、ドライブの作成を行う Createメゾットは1回だけ実行する必要があるのですが、状態を取得する方法がわからなかったので、blobにダミーファイルを作成して判断しています。
ブロブの存在確認には、Testing Existence of a Windows Azure Blob で紹介されているCloudBlobの拡張メソッドを使用しています。
using System;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;
namespace WebRole1.Storage
{
public partial class sample1 : System.Web.UI.Page
{
CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString");
protected void Page_Load(object sender, EventArgs e)
{
// コンテナがなかったら作成
string containerName = "vhd";
CloudBlobClient client = account.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference(containerName);
container.CreateIfNotExist();
// 仮想ディスク作成
int driveSize = 1000;
int cacheSize = 500;
string driveName = "drive1.vhd";
string dummyName = "drive1.dummy";
LocalResource localCache = RoleEnvironment.GetLocalResource("LocalStorage1");
CloudDrive.InitializeCache(localCache.RootPath + "cache", localCache.MaximumSizeInMegabytes);
string driveLetter = string.Empty;
CloudDrive drive = account.CreateCloudDrive(containerName + "/" + driveName);
// DriveのCreate状態を取得する方法がわからないため、blobにダミーファイルを作成
CloudBlob blob = container.GetBlobReference(dummyName);
if (!blob.Exists())
{
drive.Create(driveSize);
blob.UploadText("dummy");
}
driveLetter = drive.Mount(cacheSize, DriveMountOptions.None);
// テキストファイルを作成
string fn = driveLetter + @"\test.txt";
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fn, false, System.Text.Encoding.GetEncoding("shift_jis")))
{
sw.Write(DateTime.Now.ToString());
sw.Close();
}
}
}
}
備考
CreateCloudDriveメゾットでURIが違うと例外「ERROR_PARAMETER_INVALID parameter 0)」が発生しました。
ローカルのDevelopment FabricやDevelopment Storageを使った場合、下記ディレクトリに作成されます。
C:\Users\[ユーザー名]\AppData\Local\dftmp\wadd\devstoreaccount1\vhd\drive1.vhd
参考サイト
Windows Azure SDK 1.1 February がリリース!! waりとnaはてな日記
http://d.hatena.ne.jp/waritohutsu/20100203/1265155844
Thomas Conte's MSDN Weblog
http://blogs.msdn.com/tconte/archive/2010/04/07/using-windows-azure-drive-part-2-modifying-my-application-to-use-the-drive.aspx