【问题标题】:Authentication Failed while making REST API call to GET container details in AZURE STORAGE BLOB对 AZURE STORAGE BLOB 中的 GET 容器详细信息进行 REST API 调用时身份验证失败
【发布时间】:2021-05-31 20:33:20
【问题描述】:

我正在尝试获取 Azure 存储 Blob 中的容器详细信息。但它会抛出 Auth Failed,我认为我的资源字符串公式可能存在问题。 代码如下:

const CryptoJS = require("crypto-js");
const request = require("request");
const parser = require("xml2json");

require("dotenv").config();

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

var strTime = new Date().toUTCString();
var strToSign =
  "GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:" +
  strTime +
  `\nx-ms-version:2018-03-28\n/${account}/demo?restype:container`;
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/demo?restype=container`,

  headers: {
    Authorization: auth,
    "x-ms-date": strTime,
    "x-ms-version": "2018-03-28",
  },
};

function callback(error, response, body) {
  console.log(error);
  console.log(response.headers["Last-Modified"]);
  console.log(response);
}

request(options, callback);

在上面的示例中,demo 是我帐户中的私有容器。

【问题讨论】:

    标签: javascript node.js azure azure-blob-storage rest


    【解决方案1】:

    请尝试更改以下代码行:

    var strToSign =
      "GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:" +
      strTime +
      `\nx-ms-version:2018-03-28\n/${account}/demo?restype:container`;
    

    var strToSign =
      "GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:" +
      strTime +
      `\nx-ms-version:2018-03-28\n/${account}/demo\nrestype:container`;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      • 2011-05-23
      • 2017-11-15
      • 1970-01-01
      相关资源
      最近更新 更多