简体   繁体   中英

Azure storage authorization failed or format is wrong

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.

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);
    });
};

This request returns: 400 (Authentication information is not given in the correct format. Check the value of Authorization header.)

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

to this: 'Authorization': "SharedKey sand2storage:iDrJ7OuggJ8uoIn5olNDeOvSAoMrpqckl5mUaT5/H/w="

Then it returns: 403 (Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.)

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

NOTE:

iDrJ7OuggJ8uoIn5olNDeOvSAoMrpqckl5mUaT5/H/w=

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

从Azure站点生成密钥时的图像

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 .

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.

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);
    });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM