将Blob放入Azure存储中的睡觉API调用身份验证失败[睡觉][Azure Blob] [英] Authentication Failed in REST api call to PUT Blob in Azure Storage [REST][Azure Blob]

查看:38
本文介绍了将Blob放入Azure存储中的睡觉API调用身份验证失败[睡觉][Azure Blob]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发出PUT请求以创建Azure Blob,但它给出了AUTH FAILED。 我认为我在制定资源字符串或标头时犯了一些错误。

以下是代码

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 + '
x-ms-version:' + '2018-03-28
',
  'CanonicalizedResource': `/${account}/${containerName}/hello`
}


   var strToSign =
  string_params["verb"] +
  "
" +
  string_params["Content-Encoding"] +
  "
" +
  string_params["Content-Language"] +
  "
" +
  string_params["Content-Length"] +
  "
" +
  string_params["Content-MD5"] +
  "
" +
  string_params["Content-Type"] +
  "
" +
  string_params["Date"] +
  "
" +
  string_params["If-Modified-Since"] +
  "
" +
  string_params["If-Match"] +
  "
" +
  string_params["If-None-Match"] +
  "
" +
  string_params["If-Unmodified-Since"] +
  "
" +
  string_params["Range"] +
  "
" +
  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

推荐答案

字符串构造有问题,您需要在规范标头中添加另一个字段,即x-ms-blob类型 您的CanonicalizedHeader应如下所示:

'x-ms-blob-type:BlockBlob
x-ms-date:' + strTime + '
x-ms-version:' + '2017-04-17
',

这篇关于将Blob放入Azure存储中的睡觉API调用身份验证失败[睡觉][Azure Blob]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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