1

我编写连接到 Azure 存储帐户的 ac# 程序。

给定一个 blob URI,我需要将文件下载到本地文件并执行它。

这是我的一段代码:

var blobClientCode = client.CreateCloudBlobClient();
CloudBlockBlob codeBlob = blobClientCode.GetBlockBlobReference(codeUri);
File.Create("C:\\code.exe");
using (var fileStream = File.OpenWrite("C:\\code.exe")) {
     codeBlob.DownloadToStream(fileStream);
}

Process p = new Process();
p.StartInfo.FileName = "C:\\mycode.exe";
p.StartInfo.Arguments = dataUri;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();

问题是我不断收到 UnauthorizedAccess 异常。

  • 当我尝试从浏览器手动下载文件(复制并粘贴 URI)时,我成功了。
  • 该容器是一个公共容器。
  • 我也尝试使用 WebClient.DownloadFile(),并得到了 WebException。

我错过了什么?提前致谢

4

3 回答 3

1

在调用 webclient.DownloadFile 之前尝试包含下面提到的代码片段。希望它应该工作..

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

    ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(delegate { return true; });

 WebClient webclient = new WebClient();
webclient.DownloadFile(new Uri(URIPath), LocalPath);

注意:如果您使用代理访问互联网,您可能需要设置代理设置

WebProxy ProxyObject = ProxySetting;
webclient.Proxy = ProxySetting

;

无论证书是否经过验证,这基本上都会导航页面。

于 2013-04-14T07:37:33.793 回答
1

好的,感谢大家,我终于找到了解决方案:

我最终所做的是在角色的服务定义中定义一个本地存储,如下所示:

<LocalResources>
<LocalStorage name="myLocalStorage" sizeInMB="10" cleanOnRoleRecycle="false" />
</LocalResources>

然后只需使用此本地存储从 blob 下载文件并执行它:

LocalResource localResource = RoleEnvironment.GetLocalResource("myLocalStorage");
string PathToFile = Path.Combine(localResource.RootPath, "mycode.exe");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(delegate { return true; });
WebClient webclient = new WebClient();
webclient.DownloadFile(codeUri, PathToFile);

Process p = new Process(); //...

再次感谢大家

于 2013-04-15T10:40:44.353 回答
1

我看到您正在尝试将文件写入C:驱动器。在 Windows Azure 中,这是不允许开箱即用的。有关详细信息,请参阅此博客文章:http: //blog.codingoutloud.com/2011/06/12/azure-faq-can-i-write-to-the-file-system-on-windows-azure/。如博客文章中所述,一种选择是将 blob 保存到本地存储并从那里执行。

于 2013-04-14T10:38:12.710 回答