【问题标题】:Accessing Azure Storage Blobs from Silverlight in Firefox / Chrome在 Firefox / Chrome 中从 Silverlight 访问 Azure 存储 Blob
【发布时间】:2014-08-22 18:33:29
【问题描述】:

我正在尝试从 Silverlight 应用程序访问 Azure 存储 blob(通过 SAS 字符串)。 clientaccesspolicy.xml 存在于 $root blob 容器中,允许 Silverlight 访问。

我创建了一个示例 Silverlight 应用程序,只有一个按钮。按钮执行如下代码:

        var request = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri("http://[mystoragename].blob.core.windows.net/[mycontainername]/[myfilename]?[sasString]"));
        request.Method = "GET";
        request.BeginGetResponse(r =>
        {
            try
            {
                var response = request.EndGetResponse(r);
                using (var s = response.GetResponseStream())
                {
                    this.Dispatcher.BeginInvoke(() => MessageBox.Show(s.Length.ToString()));
                }
            }
            catch(Exception ex)
            {
                this.Dispatcher.BeginInvoke(() => MessageBox.Show(ex.Message));
            }
        }, null);

在 Internet Explorer 上运行此代码会产生预期结果 - 上传到 Azure 存储的 blob 大小。不幸的是,当在 Firefox 或 Chrome 中运行相同的应用程序时,会产生“安全错误”,而没有进一步指示可能出现的问题。

当我尝试从 PUBLIC blob 容器下载文件时会出现相同的问题(与上面的代码相同,只是 URL 不同)。公共 blob 容器也有它自己的 clientaccesspolicy.xmlcrossdomain.xml 文件(尽管我认为不需要这些文件)。再次 - 代码在 IE 中有效,但在 Firefox / Chrome 中无法运行。

可能是什么问题?我什至应该从哪里开始寻找?

【问题讨论】:

  • @SeanCocteau 我已经尝试过使用WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp); WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp); 行(因为我还希望在我的SL 应用程序中使用PUT 功能)。它仍然不起作用。至于 Fiddler,我在 ATM 上遇到了一些非常烦人的问题,除了 IE,我无法让它跟踪来自任何东西的 SL 请求。
  • 我的钱将用于研究 Silverlight 如何处理其请求并消除其中的任何问题。实现这一点的最简单方法是使用 Fiddler。

标签: c# silverlight azure azure-storage


【解决方案1】:

我找到了一个有效的解决方法。

我在托管 Silverlight 应用程序的网站上创建了一个 ASHX 处理程序。这个处理程序基本上将HttpWebRequest“重定向”到 Azure 存储。

        var url = String.Format("http://{0}.blob.core.windows.net/{1}/{2}{3}", accountName, containerName, blobName, sas);
        var request = (HttpWebRequest)WebRequest.Create(url);
        try
        {
            var response = request.GetResponse();
            context.Response.ContentType = response.ContentType;
            using (var stream = response.GetResponseStream())
            {
                stream.CopyTo(context.Response.OutputStream);
            }
        }
        catch
        {
            context.Response.End();
        }

用于构建 url 的参数作为查询字符串参数传递给 ASHX 处理程序。虽然并不理想(理想的解决方案是从 SL 直接连接到 Azure 存储),但它运行得足够快,而且似乎不会占用太多资源。

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 2021-12-18
    • 2016-05-16
    • 1970-01-01
    • 2017-09-16
    • 2019-10-14
    • 2021-01-04
    • 2017-11-18
    • 2021-11-19
    相关资源
    最近更新 更多