【问题标题】:How to prepare authorization header for azure blob storage REST API "PUT BLOB" using access keys?如何使用访问密钥为 azure blob 存储 REST API“PUT BLOB”准备授权标头?
【发布时间】:2021-10-30 22:08:22
【问题描述】:

我正在尝试使用 REST API 将 blob 上传到 azure 存储容器中(注意:容器已创建)。 但我收到错误为“状态:403,statusText:'服务器未能验证请求。确保授权标头的值正确形成,包括签名。'”

我已经准备好请求并使用解码的访问密钥进行签名,并将其转换为 base64 格式。但仍然出现错误。 我正在分享打字稿代码,请帮我找出我做错的地方。

import fetch from 'node-fetch';
import defaults  from  "../modules/defaults";
import { createHmac } from "crypto";

    async function uploadBlob(){
        
        let data = "sampletest";
        let dataLength = (data.length).toString()
        console.log("dataLength\n",dataLength)
        
       
        let accountKey = `account key looks like MSUUUMyhMSU..`;
        console.log("key\n",accountKey);
        let blob_type = "BlockBlob";
        let request_time = new Date().toUTCString();
        let api_version = "2015-02-21"
        try{
           
            let string_params = {
                'verb': 'PUT',
                'Content-Encoding': '',
                'Content-Language': '',
                'Content-Length': dataLength,
                'Content-MD5': '',
                'Content-Type': '',
                'Date': '',
                'If-Modified-Since': '',
                'If-Match': '',
                'If-None-Match': '',
                'If-Unmodified-Since': '',
                'Range': '',
                'CanonicalizedHeaders': 'x-ms-blob-type:' + blob_type + '\nx-ms-date:' + request_time + '\nx-ms-version:' + api_version,
                'CanonicalizedResource': '/' + "pcatest" +'/'+ "pca-al" + '/' + "newBlob"
            }
            
            let string_to_sign = (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'])
          
            console.log("reqHeaders \n",string_to_sign);
            let reqHeaderHash = computeHMACSHA256(string_to_sign,accountKey);
            
            console.log("==============rearheaderHash=============\n",reqHeaderHash);

            let signature = `SharedKey ${defaults.accountName}:${reqHeaderHash}`;
            console.log("==============signature=============\n",signature);
            const url = "https://test.blob.core.windows.net/testcontainer/newblob";
            console.log("url",url);
            const response = await fetch(url,{
                method:"PUT",
                body: data,
                headers : {
                    "x-ms-date" : request_time,
                    "x-ms-version" : api_version,
                    "Content-Length": dataLength,
                    "x-ms-blob-type": blob_type,
                    "Authorization" : signature
                }  
            });
            console.log("============response==============\n",response);
            }
            catch(err)
            {
                console.log("error",err);
            }
        
    }
    

function computeHMACSHA256(stringToSign: string, accountKey: string): string {
  const key = Buffer.from(accountKey, "base64")
    console.log("key\n",key);
    return createHmac("sha256", key)
        .update(stringToSign, "utf8")
        .digest("base64");
}
    uploadBlob();

【问题讨论】:

    标签: typescript authorization azure-blob-storage


    【解决方案1】:

    请检查以下几点。

    1. 如果您的共享帐户密钥正确(区分大小写)(另请查看 sas 是否已过期),请尝试通过刷新使用新密钥并重试。
    2. 检查您计算机上的时钟是否正确。您需要查看的是您的计算机时钟是否比 GMT 时间慢。 .(请参阅此thread 设置时间)。
    3. 检查 x-ms-version ,因为它可能默认指向旧版本并添加更高版本并查看。如果必须更新,请检查 szure 存储版本。
    4. 尝试使用 azure 门户中的密钥 2 生成 sas 密钥,并据此更新连接字符串。

    注意:尝试使用 SAS 密钥而不是帐户密钥,因为拥有这些凭据的任何人都可以在帐户密钥暴露时对您在该帐户中的数据执行任何他们想要的操作

    similar issue

    参考资料:

    1. About apps and programming (programtheeworld.com)

    2. Authorize with Shared Key (REST API) - Azure Storage | Microsoft Docs

    【讨论】:

    • 谢谢卡维亚。我发现问题出在格式化语法上。所以我尝试使用 azure SDK 函数进行相同的操作。然后它开始工作了。
    猜你喜欢
    • 2019-11-08
    • 1970-01-01
    • 2016-02-24
    • 2018-03-24
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    相关资源
    最近更新 更多