Azure Blob存储上的CORS错误

Azure Blob存储上的CORS错误,azure,cors,azure-storage,Azure,Cors,Azure Storage,您好,我正在尝试开发一个到Azure blob存储的文件上传机制,但是我遇到了CORS的问题。 我已经设置了一个示例上载容器。 我还为blob存储启用了CORS,至少我是这么认为的,因为下面的代码没有给我任何错误: var blobServiceProperties = AzureStorage.Default.BlobClient.GetServiceProperties(); ConfigureCors(blobServiceProperties);

您好,我正在尝试开发一个到Azure blob存储的文件上传机制,但是我遇到了CORS的问题。 我已经设置了一个示例上载容器。 我还为blob存储启用了CORS,至少我是这么认为的,因为下面的代码没有给我任何错误:

var blobServiceProperties = AzureStorage.Default.BlobClient.GetServiceProperties();
            ConfigureCors(blobServiceProperties);
            AzureStorage.Default.BlobClient.SetServiceProperties(blobServiceProperties);
在上面的代码中,AzureStorage.Default.BlobClient对象是我的Blob客户机的延迟实例化。ConfigureCors方法定义如下:

private static void ConfigureCors(ServiceProperties serviceProperties)
    {            
        serviceProperties.Cors = new CorsProperties();            
        serviceProperties.Cors.CorsRules.Add(new CorsRule()
        {
            AllowedHeaders = new List<string>() { "*" },
            AllowedMethods = CorsHttpMethods.Put | CorsHttpMethods.Get | CorsHttpMethods.Head | CorsHttpMethods.Post | CorsHttpMethods.Delete,
            AllowedOrigins = new List<string>() { 
                "https://localhost:444",
                "https://xxx.yyy.com" 
            },               
            ExposedHeaders = new List<string>() { "*" },
            MaxAgeInSeconds = 1800 // 30 minutes
        });             
    }
我正在使用Azure REST API对表服务执行操作,没有任何问题。 我还调试了C代码的执行,同时设置了CORS属性,URL正确指向云上的Azure服务,而不是开发存储。 我的第一个问题是:

当我尝试上传文件块时,根据我先前链接的示例,我得到了这个错误

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://myaccount.blob.core.windows.net/upload-73cca078-9a57-4fd4-a5b9-8012a8bb56bf?sv=2014-02-14&sr=c&si=BlobContainer&sig=mysignature&comp=block&blockid=YmxvY2stMDAwMDAw. This can be fixed by moving the resource to the same domain or enabling CORS.
Chrome控制台还显示:

XMLHttpRequest cannot load https://myaccount.blob.core.windows.net/upload-73cca078-9a57-4fd4-a5b9-8012…6mwSBkT67KIJFrTmwpSNN9slSAq0rbiLxRc%3D&comp=block&blockid=YmxvY2stMDAwMDAw. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost:444' is therefore not allowed access
我不明白,因为我已经启用了它。 我的web应用程序正在本地主机、端口444和https上运行

XHR请求如下:

function commitBlockList() {
        var uri = submitUri + '&comp=blocklist';
        console.log(uri);
        var requestBody = '<?xml version="1.0" encoding="utf-8"?><BlockList>';
        for (var i = 0; i < blockIds.length; i++) {
            requestBody += '<Latest>' + blockIds[i] + '</Latest>';
        }
        requestBody += '</BlockList>';
        console.log(requestBody);
        $.ajax({
            url: uri,
            type: "PUT",
            data: requestBody,
            beforeSend: function (xhr) {
                xhr.setRequestHeader('x-ms-blob-content-type', selectedFile.type);
                xhr.setRequestHeader('Content-Length', requestBody.length);

                xhr.setRequestHeader("x-ms-blob-type", "BlockBlob");
                xhr.setRequestHeader("x-ms-date", "Mon, 18 Aug 2014 13:05:21 GMT");
                xhr.setRequestHeader("x-ms-version", "2012-02-12");
                xhr.setRequestHeader("Authorization", "SharedKey myaccount:d3gXfj6kSp4qVejioDjKQA7dbPyFerDf2iw47RcmGXM=");
            },
            success: function (data, status) {
                console.log(data);
                console.log(status);
            },
            error: function (xhr, desc, err) {
                console.log(xhr);
                console.log(desc);
                console.log(err);
            }
        });

    }

请确保在设置容器权限后等待30秒,以确保更改生效,并且使用策略ID生成的SA有效


此外,您的代码首先检查策略ID是否存在,如果已经存在,则不进行更新。使用该ID生成的SAS令牌将在您首次添加该策略后1天停止工作,因为它永远不会使用新的过期日期/时间更新该策略。

好的,似乎在对Azure进行PUT API调用时,我必须将blob引用添加到URI

var getUploadContainerUrl = function (file) {            
            var usad = config.accessData.blob;
            var url = usad.uri + "/" + file.name + usad.sharedAccessSignature;
            return url;
        }
然后,我的submitUri javascript变量被这样检索

submitUri = getUploadContainerUrl(file)

其中文件是正在上载的文件。我没有在url路径中包含file.name属性。无论如何,我得到的错误是误导性的,因为它引用了CORS,而CORS是正确启用的。

我等待了30秒甚至更长时间,更改了代码以设置POLICY see edited问题,但没有任何更改。我的怀疑是,这甚至不是一个容器权限问题,而是CORS的问题。我还调试了我用来设置CORS属性的“SetServiceProperty”,我发现URL是正确的,而且我在https://localhost:444上运行应用程序的URL甚至在那里,所以设置了CORS属性。但是我不知道为什么它仍然给我这个CORS错误。
var getUploadContainerUrl = function (file) {            
            var usad = config.accessData.blob;
            var url = usad.uri + "/" + file.name + usad.sharedAccessSignature;
            return url;
        }
submitUri = getUploadContainerUrl(file)