Ошибка аутентификации при вызове API REST для PUT Blob в хранилище Azure [REST] [Azure Blob]

Я пытаюсь сделать запрос PUT для создания большого двоичного объекта Azure, но он дает ошибку аутентификации. Я думаю, что делаю некоторые ошибки при формулировании строки ресурса или заголовков.

Вот код

const CryptoJS = require("crypto-js");
const request = require("request");
require("dotenv").config();
var parser = require("xml2json");

const account = process.env.ACCOUNT_NAME || "";
const key = process.env.ACCOUNT_KEY || "";
var containerName = "demo";

var strTime = new Date().toUTCString();

string_params = {
  'verb': 'PUT',
  'Content-Encoding': '',
  'Content-Language': '',
  'Content-Length': 11,
  'Content-MD5': '',
  'Content-Type': 'text/plain; charset=UTF-8',
  'Date': '',
  'If-Modified-Since': '',
  'If-Match': '',
  'If-None-Match': '',
  'If-Unmodified-Since': '',
  'Range': '',
  'CanonicalizedHeaders': 'x-ms-date:' + strTime + '\nx-ms-version:' + '2018-03-28\n',
  'CanonicalizedResource': `/${account}/${containerName}/hello`
}


   var strToSign =
  string_params["verb"] +
  "\n" +
  string_params["Content-Encoding"] +
  "\n" +
  string_params["Content-Language"] +
  "\n" +
  string_params["Content-Length"] +
  "\n" +
  string_params["Content-MD5"] +
  "\n" +
  string_params["Content-Type"] +
  "\n" +
  string_params["Date"] +
  "\n" +
  string_params["If-Modified-Since"] +
  "\n" +
  string_params["If-Match"] +
  "\n" +
  string_params["If-None-Match"] +
  "\n" +
  string_params["If-Unmodified-Since"] +
  "\n" +
  string_params["Range"] +
  "\n" +
  string_params["CanonicalizedHeaders"] +
  string_params["CanonicalizedResource"];

var secret = CryptoJS.enc.Base64.parse(key);
var hash = CryptoJS.HmacSHA256(strToSign, secret);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
var auth = `SharedKey ${account}:` + hashInBase64;

const options = {
  url: `https://${account}.blob.core.windows.net/${containerName}/hello`,
  headers: {
    Authorization: auth,
    "x-ms-date": strTime,
    "x-ms-version": "2018-03-28",
    "x-ms-blob-type": "BlockBlob",
    "Content-Length": "11",
    "Content-Type": "text/plain; charset=UTF-8",
  },
  body: "hello world",
};

function callback(error, response, body) {
  console.log(response.statusCode);
  console.log(response.statusMessage);
  console.log(response.headers);
}

request.put(options, callback);

При запуске приведенного выше кода я получаю следующее сообщение:

403
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

Ссылки на то же самое можно найти здесь: https://docs.microsoft.com/en-us/rest/api/storageservices/put-blob https://docs.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key


person Community    schedule 03.03.2021    source источник


Ответы (1)


Возникла проблема с формированием строки, вам нужно добавить в канонический заголовок еще одно поле типа x-ms-blob-type ваши CanonicalizedHeaders должны выглядеть следующим образом:

'x-ms-blob-type:BlockBlob\nx-ms-date:' + strTime + '\nx-ms-version:' + '2017-04-17\n',
person Akshay Vashishtha    schedule 03.03.2021