azure - 上传到 Azure Blob 时请求的资源上不存在 'Access-Control-Allow-Origin' header

标签 azure cors azure-blob-storage

我已经为此苦苦挣扎了大约一天。我正在直接测试 Azure Blob 存储上传并遇到可怕的 CORS 问题。 “XMLHttpRequest无法加载https://tempodevelop.blob.core.windows.net/tmp/a4d8e867-f13e-343f-c6d3-a603…Ym0PlrBn%2BU/UzUs7QUhQw%3D&sv=2014-02-14&se=2016-10 -12T17%3A59%3A26.638531。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin” header 。来源“http://localhost:8000”是因此不允许访问。响应的 HTTP 状态代码为 403。”

我已经尝试过的事情:

  1. 将 CORS 设置为所有主机: enter image description here
  2. 尝试在本地和 Heroku 上托管我的应用
  3. 确保我可以使用其他工具(Azure 存储资源管理器)上传文件
  4. 将我的 AccessPolicy 配置为“rwdl”,我肯定会获得访问签名(在单元测试中验证)。

整个代码可以在这里找到:https://github.com/mikebz/azureupload

但是相关部分在这里,前端上传:

<script>

    /* 
     * not a true GUID, see here: http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
     */ 
    function guid() {
        function s4() {
            return Math.floor((1 + Math.random()) * 0x10000)
            .toString(16)
            .substring(1);
        }
        return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
            s4() + '-' + s4() + s4() + s4();
    }

    function startUpload() {   
        var fileName = guid();

        jQuery.getJSON("/formfileupload/signature/" + fileName , function(data) {
                console.log("got a signature: " + data.bloburl);
                uploadFile(data.bloburl, data.signature);
            })
            .fail(function(jqxhr, textStatus, error) {
                console.log( "error: " + textStatus + " - " + error );
            })
    }
    
    function uploadFile(bloburl, signature) {        
        var xhr = new XMLHttpRequest();
        fileData = document.getElementById('fileToUpload').files[0];
        xhr.open("PUT", bloburl + "?" + signature);
        xhr.setRequestHeader('x-ms-blob-type', 'BlockBlob');
        xhr.setRequestHeader('x-ms-blob-content-type', fileData.type);
        result = xhr.send(fileData);
    }
</script>

Python中的签名生成代码在这里:

def generate_access_signature(self, filename):
    """
    calls the Azure Web service to generate a temporary access signature.
    """
    blob_service = BlobService(
        account_name=self.account_name, 
        account_key=self.account_key
    )

    expire_at = datetime.utcnow()
    expire_at = expire_at + timedelta(seconds = 30)
    access_policy = AccessPolicy(permission="rwdl", expiry=expire_at.isoformat())

    sas_token = blob_service.generate_shared_access_signature( 
        container_name="tmp",
        blob_name = filename, 
        shared_access_policy=SharedAccessPolicy(access_policy)
    )
    return sas_token

最佳答案

根据错误消息[响应的 HTTP 状态代码为 403],可能是服务未启用 CORS,或者没有与预检请求匹配的 CORS 规则。详情请引用Cross-Origin Resource Sharing (CORS) Support for the Azure Storage Services 。 或者可能是 SAS 签名不正确。 请尝试排除故障

  1. 尝试检查 Azure 门户上 Blob 服务下的 CORS 设置。因为还有其他服务,如表、队列、文件。 Expected

  2. 还有Azure explore可用于生成 SAS token 的工具 enter image description here

获取 SAS 并尝试使用生成的 SAS 进行调试 demo code enter image description here

关于azure - 上传到 Azure Blob 时请求的资源上不存在 'Access-Control-Allow-Origin' header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40005725/

相关文章:

Azure 表更新并发问题

android - 使用 SAS(共享访问签名)通过 Azure 存储上传文件

azure - 当还配置了 SSO 时,无法使用 Microsoft 帐户添加更多共同管理员

java - Spring 启动+安全: No Access-Control-Allow-Origin header when setting allowed origins

javascript - Firefox CORS 与 JSON 的问题

jquery - Bottle Py : Enabling CORS for jQuery AJAX requests

azure - 将 Python 日志记录设置为 Azure Blob,但在那里找不到日志文件

azure - 使用电子邮件搜索现有用户时,AAD 请求 https ://graph. windows.net 方法 GET 因为请求正文格式错误

Azure虚拟机: SQL servers on machines should have vulnerability findings resolved

azure - 如何将 az keyvault Secret list -o table 输出存储在数组中? Azure CLI、Shell 脚本