Azure 存储授权失败或格式错误 [英] Azure storage authorization failed or format is wrong

查看:27
本文介绍了Azure 存储授权失败或格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我一直在使用 Azure 存储授权.当我只上传一个文件(blob)时,我将 SAS 密钥放在 url 中,一切正常.但是当我需要在 BLOB 服务中创建块时,创建它们时必须有授权标头(亚马逊说).我正在尝试使用 本文 创建授权.

Good day everybody, I'm stuck with authorization on azure storage. When I am uploading only one file(blob) I put SAS key in the url and everything works fine. But when I need to create chunks in BLOB service, there must be Authorization header(Amazon says that) when creating them. I am trying to use this article to create authorization.

有我的 JavaScript 代码(我使用 ng-file-uploader 库):

There is my JavaScript code(I use ng-file-uploader library):

    $scope.upload = function (file) {
    blockId = blockId + 1;
    Upload.upload({
        url: "https://MYSTORAGENAME.blob.core.windows.net/kont1/"+ file.name + "?comp=block&blockid=" + blockId,
        method: 'PUT',
        resumeChunkSize: '40MB', // upload in chunks of specified size
        headers: {
            'Content-type': 'multipart/form-data',
            'Authorization': 'SharedKey' + "MYSTORAGENAME:iDrJ7OuggJ8uoIn5olNDeOvSAoMrpqckl5mUaT5/H/w=",
            'x-ms-version': '2015-12-11',
            'x-ms-date': new Date().toUTCString()
            },
        data: {file: file}
    }).then(function (resp) {
        console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
    }, function (resp) {
        console.log('Error status: ' + resp.status);
    }, function (evt) {
        var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
        console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
    });
};

此请求返回:400(未以正确格式提供身份验证信息.请检查授权标头的值.)

当我尝试改变这一点时:'授权':'SharedKey' + "MYSTORAGENAME:iDrJ7OuggJ8uoIn5olNDeOvSAoMrpqckl5mUaT5/H/w="

When I try to change this: 'Authorization': 'SharedKey' + "MYSTORAGENAME:iDrJ7OuggJ8uoIn5olNDeOvSAoMrpqckl5mUaT5/H/w="

对此:'授权':SharedKey sand2storage:iDrJ7OuggJ8uoIn5olNDeOvSAoMrpqckl5mUaT5/H/w="

然后返回:403(服务器验证请求失败.确保Authorization header的值包括签名正确形成.)

我在这上面花了 2 天的时间,但仍然无法解决问题,也许有人看到了我缺少的东西?提前致谢.

I spent 2days on this and still can't get it right, maybe somebody see's what I am missing? Thank you in advance.

注意:

iDrJ7OuggJ8uoIn5olNDeOvSAoMrpqckl5mUaT5/H/w=

iDrJ7OuggJ8uoIn5olNDeOvSAoMrpqckl5mUaT5/H/w=

我在 Azure 页面中生成此密钥,每次都尝试确保该密钥正确.

I generate this key in Azure page each try to make it sure that key is correct.

推荐答案

如果您使用的是 Shared Access Signature (SAS),则无需指定 Authorization 标头,因为 SAS 令牌包含此值.您也不需要定义 x-ms-versionx-ms-date 标头.您需要包含的是 x-ms-blob-type 请求标头并将其值设置为 BlockBlob.

If you're using Shared Access Signature (SAS), then you don't need to specify the Authorization header as the SAS token contains this value. You also need not define x-ms-version and x-ms-date headers. What you do need to include is x-ms-blob-type request header and set its value to BlockBlob.

您需要做的是获取 SAS 令牌并将其附加到您的 URL(请确保您没有在 SAS 令牌中包含 ?.

What you would need to do is take the SAS token and append that to your URL (please make sure that you don't include the ? in the SAS Token.

假设您将来自门户的 Sas 令牌存储在名为 sasToken 的变量中,您的代码将是:

Assuming you're storing the Sas Token from portal in a variable called sasToken, your code would be:

$scope.upload = function (file) {
    blockId = blockId + 1;
    Upload.upload({
        url: "https://MYSTORAGENAME.blob.core.windows.net/kont1/"+ file.name + "?comp=block&blockid=" + blockId + "&" + sasToken,
        method: 'PUT',
        resumeChunkSize: '40MB', // upload in chunks of specified size
        headers: {
            'Content-type': 'multipart/form-data',
            'Authorization': 'SharedKey' + "MYSTORAGENAME:iDrJ7OuggJ8uoIn5olNDeOvSAoMrpqckl5mUaT5/H/w=",
            'x-ms-version': '2015-12-11',
            'x-ms-date': new Date().toUTCString()
            },
        data: {file: file}
    }).then(function (resp) {
        console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
    }, function (resp) {
        console.log('Error status: ' + resp.status);
    }, function (evt) {
        var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
        console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
    });
};

这篇关于Azure 存储授权失败或格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆