从Chrome应用程序中使用JQuery获取Azure Blob

从Chrome应用程序中使用JQuery获取Azure Blob,jquery,cors,google-chrome-app,azure-storage-blobs,Jquery,Cors,Google Chrome App,Azure Storage Blobs,我正在尝试使用以下jquery代码从我正在开发的Chrome应用程序执行一个简单的GET请求,以在我的Azure存储上创建一个blob: $.ajax({ headers: { 'x-ms-range': 'bytes=' + from + '-' + to, 'x-ms-version': version, 'x-ms-client-request-id': guid() },

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

$.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) {
});
当我尝试时,会出现以下错误:

选择权

无法加载XMLHttpRequest。对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头。起源铬-extension://blablabla因此,不允许访问。响应的HTTP状态代码为403

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

谢谢

更新

我通过以下代码进行了尝试:

$.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) {
});
$.ajax({
网址:'https://docbetaeustorage.blob.core.windows.net/?restype=service&comp=properties',
标题:{
“内容类型”:“应用程序/xml”,
“x-ms-date”:新日期(),
“x-ms-version”:“2013-08-15”,
“授权”:“共享密钥”
},
键入:“放置”,
数据:'+THIS_URL+'GET,PUT500x-ms-*x-ms-*'
}).完成(功能(数据){
}).失败(功能(错误){
});

现在我得到了这个请求的飞行前错误…

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

您可以在此处找到详细信息:

以下代码仅与C#开发者相关。azure中有RESTAPI来启用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);
        }
where storageAccount-您的存储帐户

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


我将requestOptions和operationContext保留为null,您不必为此使用任何自定义库。您所需要的只是在Blob存储中启用CORS

您可以在此处找到详细信息:

以下代码仅与C#开发者相关。azure中有RESTAPI来启用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);
        }
where storageAccount-您的存储帐户

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


我将requestOptions和operationContext保持为null,我让它正常工作了。。。我不知道CORS的设置是一次性的

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

我只在C#中运行了一次以下代码:

命名空间启用CorsinAzure
{
班级计划
{
静态void Main(字符串[]参数)
{
var connString=“--MY-CONNECTION-STRING-FROM-AZURE-PORTAL--”;
var-storageAccount=CloudStorageAccount.Parse(connString);
var blobClient=storageAccount.CreateCloudBlobClient();
ServiceProperties ServiceProperties=blobClient.GetServiceProperties();
serviceProperties.Cors=新的CorsProperties();
serviceProperties.Cors.CorsRules.Add(新的CorsRule()
{
AllowedHeaders=new List(){“*”},
ExposedHeaders=new List(){“*”},
AllowedMethods=CorsHttpMethods.Post | CorsHttpMethods.Put | CorsHttpMethods.Get | CorsHttpMethods.Delete,
AllowedOrigins=新列表(){“*”},
MaxAgeInSeconds=3600,
});
blobClient.SetServiceProperties(serviceProperties);
}
}
}

虽然包括了WindowsAzure.Storagenuget.

但我还是让它工作了。。。我不知道CORS的设置是一次性的

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

我只在C#中运行了一次以下代码:

命名空间启用CorsinAzure
{
班级计划
{
静态void Main(字符串[]参数)
{
var connString=“--MY-CONNECTION-STRING-FROM-AZURE-PORTAL--”;
var-storageAccount=CloudStorageAccount.Parse(connString);
var blobClient=storageAccount.CreateCloudBlobClient();
ServiceProperties ServiceProperties=blobClient.GetServiceProperties();
serviceProperties.Cors=新的CorsProperties();
serviceProperties.Cors.CorsRules.Add(新的CorsRule()
{
AllowedHeaders=new List(){“*”},
ExposedHeaders=new List(){“*”},
AllowedMethods=CorsHttpMethods.Post | CorsHttpMethods.Put | CorsHttpMethods.Get | CorsHttpMethods.Delete,
AllowedOrigins=新列表(){“*”},
MaxAgeInSeconds=3600,
});
blobClient.SetServiceProperties(serviceProperties);
}
}
}

同时包括
WindowsAzure.Storage
nuget.

@shlatchz-Me太。。上面的代码是通过C#启用cors的,但我明确表示我使用的是javascript JQuery。我尝试发出url中描述的put请求,现在我得到了该请求的飞行前错误。@Developer…在razor web应用程序中,您在Startup?Developer…中运行该请求,CorsRequest类来自哪个库?@dinotom-我有一个用于Blob的自定义包装器客户端(使我的代码不受Azure Blob存储、本地文件存储和AWS S3的影响)我在静态构造函数中完成了这项工作。顺便说一句,您现在可以通过Azure门户设置corsitself@shlatchz-我也是..上面的代码是通过C#启用cors的。但我明确表示我使用的是javascript JQuery。我尝试发出url中描述的put请求,现在我在该请求上得到了飞行前错误。@Developer…在razor web应用程序中你在Startup中运行这个吗?@Developer…CorsRequest类来自哪个库?@dinotom-我有一个自定义的Blob包装客户端(使我的代码不受Azure Blob存储、本地文件存储和AWS S3的影响),我是在静态构造函数中运行的。顺便说一句,你现在可以通过Azure门户itselfJus设置cors