Cloudservie将LocalStroage中的内容通过WAD自动上传到BLOB中

开发云服务程序,如果使用Local stroage存储我们临时生成的日志或者文件并将它们自动上传到BLOB中,可以通过WAD来实现,具体如下:

1.配置webrole,开启Local stroage功能:

 

2.修改wadcfgx文件,添加DataSources属性:

3.添加上传到local storage的代码:

Index.cshtml

@{
    ViewBag.Title = "Home Page";
}
<div class="jumbotron">
    <h1>ASP.NET</h1>
    <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
    <p><a href="http://asp.net" class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
</div>
<div class="row">
    <div class="col-md-12">
        @using (Html.BeginForm("UploadZipFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <div class="form-group">
                <label for="zipFile">Zip File</label>
                <input type="file" id="zipFile" name="zipFile">
            </div>
            <button type="submit" class="btn btn-default">Submit</button>
        }
    </div>
</div>

对应的HomeController.cs实现如下:

        public ActionResult UploadZipFile(HttpPostedFileBase zipFile)
        {
            var localResource = RoleEnvironment.GetLocalResource("ZipFiles");

            // Save zip file.
            var localZipFilePath = Path.Combine(localResource.RootPath, zipFile.FileName);
            zipFile.SaveAs(localZipFilePath);

            // Extract zip file to a random folder.
            var localExtractedPath = Path.Combine(localResource.RootPath, Guid.NewGuid().ToString());
            Directory.CreateDirectory(localExtractedPath);
            System.IO.Compression.ZipFile.ExtractToDirectory(localZipFilePath, localExtractedPath);

            // Upload everything to blob storage.
            foreach (var file in Directory.GetFiles(localExtractedPath, "*", SearchOption.AllDirectories))
            {
                // Upload to blob storage.
            }

            return RedirectToAction("Index");
        }

获取Local Storege的路径是:var localResource = RoleEnvironment.GetLocalResource("ZipFiles");

3.我在本地做了测试,通过代码上传到设置的Local Storage后,通过RDP登陆实例,会看到文件已经存在了,路径是C:\Resources\Directory\{部署ID}.WebRole1.ZipFiles

 

4.一旦指定的Local Storage有了内容,对应的存储账号会生成容器wad-testcustom(这也是自己设置的名称),当日志自动上传成功后,会看到对应的存储账号容器中,上传了之前测试的文件:

 

具体可以参考:http://blogs.msdn.com/b/davidhardin/archive/2011/03/31/configuring-diagnostics-wadcfg-to-capture-custom-log-files.aspx

关于Local Storage,就算不勾选"Clean on role recycle",在节点故障转移的时候,如果有的日志还没有及时存到BLOB中,我们还是会丢掉它们,具体可以参考:

http://justazure.com/microsoft-azure-cloud-services-part-3-service-package/

 

posted @ 2015-08-03 19:11  曾经的漂移  阅读(315)  评论(0编辑  收藏  举报