【问题标题】:GET Azure Blob using JQuery from a Chrome App从 Chrome 应用程序中使用 JQuery 获取 Azure Blob
【发布时间】:2016-08-24 09:44:45
【问题描述】:

我正在尝试使用以下 jquery 代码从我正在开发的 Chrome 应用程序向我的 Azure 存储上的 blob 执行一个简单的 GET 请求:

$.ajax({
        headers: {
            'x-ms-range': 'bytes=' + from + '-' + to,
            'x-ms-version': version,
            'x-ms-client-request-id': guid()
        },
        url: 'https://storage-name.blob.core.windows.net/container-id/blob-id?sv=2015-07-08&sr=b&sig=signature&st=2016-08-24T08%3A58%3A30Z&se=2016-08-24T09%3A28%3A30Z&sp=rl&api-version=2015-07-08&',
        type: "GET",
    }).done(function (data) {
    }).fail(function (error) {
});

当我尝试时,我收到以下错误:

选项https://storage-name.blob.core.windows.net/container-id/blob-id?sv=2015-07-08&sr=b&sig=signature&st=2016-08-24T08%3A58%3A30Z&se=2016-08-24T09%3A28%3A30Z&sp=rl

XMLHttpRequest 无法加载 https://storage-name.blob.core.windows.net/container-id/blob-id?sv=2015-07-08&sr=b&sig=signature&st=2016-08-24T08%3A58%3A30Z&se=2016-08-24T09%3A28%3A30Z&sp=rl。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问 Origin 'chrome-extension://blablabla'。响应的 HTTP 状态代码为 403。

请帮忙!我尝试了很多 CORS 选项,但没有一个对我有用。 我真的想保留一个简单的 ajax 请求,并避免为该请求使用自定义库。

谢谢!

更新

我通过以下代码尝试了enabling CORS

$.ajax({
        url: 'https://docbetaeustorage.blob.core.windows.net/?restype=service&comp=properties',
        headers: {
          'Content-Type': 'application/xml',
          'x-ms-date': new Date(),
          'x-ms-version': '2013-08-15',
          'Authorization': 'SharedKey'
        },
        type: "PUT",
        data: '<?xml version="1.0" encoding="utf-8"?><StorageServiceProperties><Cors><CorsRule><AllowedOrigins>' + THIS_URL + '</AllowedOrigins><AllowedMethods>GET,PUT</AllowedMethods><MaxAgeInSeconds>500</MaxAgeInSeconds><ExposedHeaders>x-ms-*</ExposedHeaders><AllowedHeaders>x-ms-*</AllowedHeaders></CorsRule></Cors></StorageServiceProperties>'
    }).done(function (data) {
    }).fail(function (error) {
});

现在我收到此请求的预检错误...

【问题讨论】:

  • 只是为了测试目的,您可以尝试将 AllowedOrigins 、 ExposedHeaders 和 AllowedHeaders 设置为 * 吗?
  • 没关系......它仍然落在预检中。
  • 几个愚蠢的疑问 - 根据文档,'x-ms-date' 应该是 UTC - 这会是个问题吗?你确定x-ms-version吗? Azure 存储服务的当前版本是 2015-12-11
  • 也不适用于 UTC,这是根据 msdn.microsoft.com/en-us/library/azure/hh452235.aspx 的版本。我认为 put 请求甚至没有被处理...它卡在自动生成的 OPTIONS 请求上...
  • OPTIONS 飞行前请求将为 PUT 和 DELETE 跨域请求以及其他动词生成,以防有自定义标头并且内容类型不在允许的类型范围内。因此,如果您在该请求中通过了良好的身份验证,则预检应该不是问题

标签: jquery cors google-chrome-app azure-blob-storage


【解决方案1】:

您不必为此使用任何自定义库。您只需要在 Blob 存储中启用 CORS。

您可以在此处找到详细信息: https://blogs.msdn.microsoft.com/windowsazurestorage/2014/02/03/windows-azure-storage-introducing-cors/

以下代码仅适用于 C# 开发人员。有 REST api 可以在 azure 中启用 CORS

这就是我通过 Blob 帐户中的 C# 代码启用 CORS 的方式:

public void EnableCors(CloudStorageAccount storageAccount, CorsRequest corsRequest, IRequestOptions requestOptions=null, OperationContext operationContext = null)
        {
            var serviceTypeClient = storageAccount.CreateCloudBlobClient();

            ServiceProperties serviceProperties = new ServiceProperties();

            // Nullifying un-needed properties so that we don't 
            // override the existing ones 
            serviceProperties.HourMetrics = null;
            serviceProperties.MinuteMetrics = null;
            serviceProperties.Logging = null;


            serviceProperties.Cors.CorsRules.Add(new CorsRule()
            {
                AllowedHeaders = corsRequest.AllowedHeaders,
                ExposedHeaders = corsRequest.ExposedHeaders,
                AllowedMethods = corsRequest.AllowedMethods,
                AllowedOrigins = corsRequest.AllowedOrigins,
                MaxAgeInSeconds = corsRequest.PreFlightRequestAgeInMins * 60,
            });
            serviceTypeClient.SetServiceProperties(serviceProperties, requestOptions as BlobRequestOptions, operationContext);
        }

其中 storageAccount - 您的存储帐户

corsRequest - 只是我从配置文件中读取的所需值

我将 requestOptions 和 operationContext 保持为空

【讨论】:

  • @shlatchz - 我也是.. 上面的代码是通过 C# 启用 cors。
  • 但我明确表示我使用的是 javascript JQuery。我尝试发出 url msdn.microsoft.com/en-us/library/azure/hh452235.aspx 中描述的 put 请求,现在我收到了该请求的预检错误。
  • @Developer...在 Razor Web 应用程序中,您在 Startup 中运行它吗?
  • @Developer...CorsRequest 类来自哪个库?
  • @dinotom - 我有一个 Blob 的自定义包装客户端(使我的代码与 Azure Blob 存储、本地文件存储和 AWS S3 无关),我在静态构造函数中执行了此操作。顺便说一句,您现在可以通过 Azure 门户本身设置 cors
【解决方案2】:

嗯,我搞定了……我不知道 CORS 设置是一次性的。

我从 azure 门户获取了存储帐户的连接密钥:

我刚刚在 C# 中运行了以下代码:

namespace EnableCorsInAzure
{
    class Program
    {
        static void Main(string[] args)
        {
            var connString = "--MY-CONNECTION-STRING-FROM-AZURE-PORTAL--";
            var storageAccount = CloudStorageAccount.Parse(connString);
            var blobClient = storageAccount.CreateCloudBlobClient();
            ServiceProperties serviceProperties = blobClient.GetServiceProperties();
            serviceProperties.Cors = new CorsProperties();
            serviceProperties.Cors.CorsRules.Add(new CorsRule()
            {
                AllowedHeaders = new List<string>() { "*" },
                ExposedHeaders = new List<string>() { "*" },
                AllowedMethods = CorsHttpMethods.Post | CorsHttpMethods.Put | CorsHttpMethods.Get | CorsHttpMethods.Delete,
                AllowedOrigins = new List<string>() { "*" },
                MaxAgeInSeconds = 3600,
            });
            blobClient.SetServiceProperties(serviceProperties);
        }
    }
}

同时包括WindowsAzure.Storagenuget。

【讨论】:

  • 这不是我们一开始讨论的吗?
  • 正如我所说,我不知道我只需要做一次。我认为必须在每个请求之前完成。
猜你喜欢
  • 1970-01-01
  • 2019-05-09
  • 2018-12-24
  • 2020-06-27
  • 2014-10-29
  • 2023-03-31
  • 2011-05-22
  • 2022-11-06
  • 1970-01-01
相关资源
最近更新 更多