因此,我一直在从事一个项目,在该项目中,我希望为Azure Table Storage构建像CRUD这样的基本功能,但是我一直坚持生成有效的SharedKeyLite签名。

我能够为GET和POST生成有效的签名,但是当我出于某种奇怪的原因想要PUT或DELETE和实体时,签名现在无效吗?

const axios = require('axios');

const client = axios.create({});

const SharedKeyGenerate = require('./SharedKeyGenerator');

const AZURE_STORAGE_KEY = 'STORAGE_KEY';
const AZURE_STORAGE_ACCOUNT = 'igdevharuntest';

const sharedKey = new SharedKeyGenerate(AZURE_STORAGE_ACCOUNT, 'powerbiTableStorage', AZURE_STORAGE_KEY);

client.defaults.baseURL = `https://${AZURE_STORAGE_ACCOUNT}.table.core.windows.net`;
client.defaults.timeout = 2000;

/**
 * get tables from azure table storage
 * with optional filter
 * @param {string} path uri path
 * @param {string} filter filter for the web request
 */
async function getTables(path, filter) {
    try {
        const response = await client.get(`/${path}${filter}`, {
            headers: {
                'x-ms-date': new Date().toUTCString(),
                'Authorization': sharedKey.GenerateSharedKeyLite(path, 'GET', client),
                Accept: 'application/json;odata=nometadata',
                'x-ms-version': '2015-12-11',
            }
        });

        console.log(response.data);
    } catch (err) {
        console.log(err.response.data);
    }
}

async function addEntityToTable(tableName) {
    const payload = {
        'expiresOn': '2019-10-16T17:27:36.046Z',
        'accessToken': 'abcsd123456',
        'PartitionKey': 'NewPartitionKey12',
        'RowKey': 'CompletelyNewKey12'
    };


    try {
        const response = await client.post(`/${tableName}`, payload,{
            headers: {
                'x-ms-date': new Date().toUTCString(),
                'Authorization': sharedKey.GenerateSharedKeyLite(tableName, "POST"),
                Accept: 'application/json;odata=nometadata',
                'x-ms-version': '2015-12-11',
            }
        });

        console.log(response.data);
    } catch (err) {
        console.log(err.response);
    }
}

async function updateEntity(path, filter) {
    const payload = {
        "PartitionKey": "ABCDEFG1234567RANDOMPARTITIONKEY1111",
        "RowKey": "ABCDEFG1234567",
        "expiresOn": "2019-10-16T17:37:07.099Z",
        "accessToken": "eyJ0eXAiOiJKV1QiLCjjJhbGciOiJ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XRlLkFsbCBEYXRhc2V0LlJlYWQuQWxsIERhdGFzZXQuUmVhZFdyaXRlLkFsbCBHcm91cC5SZWFkIEdyb3VwLlJlYWQuQWxsIE1ldGFkYXRhLlZpZXdfQW55IFJlcG9ydC5SZWFkLkFsbCBSZXBvcnQuUmVhZFdyaXRlLkFsbCBXb3Jrc3BhY2UuUmVhZC5BbGwgV29ya3NwYWNlLlJlYWRXcml0ZS5BbGwiLCJzdWIiOiJkQzlDN1hSbnktdHcyWGc2QkRqYUZULWg3cmo1T05lN2VkOWV6dTZqWmQ4IiwidGlkIjoiMWMxNzY3MTItMTVmNy00MThjLTk1YWMtNDZmNmE2YTU5NWM1IiwidW5pcXVlX25hbWUiOiJoc2hlaWtoYWxpQGlnbG9vc29mdHdhcmUuY29tIiwidXBuIjoiaHNoZWlraGFsaUBpZ2xvb3NvZnR3YXJlLmNvbSIsInV0aSI6ImxhYUd6UWVKRDBxZFVTbDZtRjRmQUEiLCJ2ZXIiOiIxLjAifQ.prKqNPn75_CWdbLvsl5VVvuZAK-PEI2n1DlU4gFayt_eLPzllZUlEpVqIgVgTAzeccYEj5Z6vBKpMjXT7ftwCVjnKQitidGILehaEfrWiXX3xU4ZatPQ_TNc6Y6NzMyIQTWAbPkCHfpFnBlbAD0xp9Kl-bpAq_QXbl4yIa6_IQMRMwi5WdWd8WJLLdxKQTkWiKkGBBl-La3wgYrWfzBXMzBLhlfMk_vqsOyJdg1jOUEnUmScqxKh5DUR5DvoRtdeVxc2rDz1GWM8MTdhviB0CRub7bKeMA35rLzEui69L4o8gT_FuXLXqvVLDL9sq7OZNX8q3BL-VxLkq6GSOpnkcg"
    };


    try {
        const response = await client.put(`/${path}${filter}`, payload,{
            headers: {
                'x-ms-date': new Date().toUTCString(),
                'Accept-Charset': 'UTF-8',
                'Authorization': sharedKey.GenerateSharedKeyLite(path, "PUT"),
                Accept: 'application/json;odata=nometadata',
                'x-ms-version': '2015-12-11',
                'Content-Type': 'application/json',
                'If-Match': '*'
            }
        });

        console.log(response);
    } catch (err) {
        console.log(err.response.data);
    }
}

async function deleteEntity(path, filter, partitionKey, rowKey) {
    try {
        const response = await client.delete(`/${path}${filter}`, {
            headers: {
                'x-ms-date': new Date().toUTCString(),
                'Accept-Charset': 'UTF-8',
                'Authorization': sharedKey.GenerateSharedKeyLite(path),
                Accept: 'application/json;odata=nometadata',
                'x-ms-version': '2015-12-11',
                'Content-Type': 'application/json',
                'If-Match': '*'
            }
        });

        console.log(response);
    } catch (err) {
        console.log(err.response.data);
    }
}

//Works
getTables("powerbiTableStorage()", "?$filter=PartitionKey%20eq%20'ABCDEFG1234567RANDOMPARTITIONKEY1111'%20and%20RowKey%20eq%20'ABCDEFG1234567'");


//Works
addEntityToTable('powerbiTableStorage()');


//does not work
updateEntity("powerbiTableStorage(PartitionKey='ABCDEFG1234567RANDOMPARTITIONKEY1111',RowKey='ABCDEFG1234567')", "?$filter=PartitionKey%20eq%20'ABCDEFG1234567RANDOMPARTITIONKEY1111'%20and%20RowKey%20eq%20'ABCDEFG1234567'");


//does not work
deleteEntity("powerbiTableStorage(PartitionKey='ABCDEFG1234567RANDOMPARTITIONKEY1111',RowKey='ABCDEFG1234567')", "");



这是错误消息:
{
  'odata.error':{
    代码:“ AuthenticationFailed”,
    信息: {
      lang:“ zh-CN”,
      值:“服务器无法验证请求。确保正确构成Authorization标头的值,包括签名。\ n'+
        'RequestId:d5684fff-8002-001c-0dd9-9fe2c5000000 \ n'+
        '时间:2019-11-20T19:31:28.7740552Z'
    }
  }
}

有人可以帮我吗?

快速更新...

这是我运行GET请求时得到的

stringToSign:
2019年11月21日星期四15:48:07 GMT
/ igdevharuntest /表

签名:
SharedKeyLite igdevharuntest:PxpjkL + WwN7ZtHD1NXctjtMKSdWuAjNY3xS3jo7 / n / Q =

{
  value: [
    { TableName: 'photowallCache' },
    { TableName: 'powerbiTableStorage' },
    { TableName: 'tblProjectStatus' }
  ]
}


共享密钥生成:

const Utf8 = require('crypto-js/enc-utf8');
const Base64 = require('crypto-js/enc-base64');
const hmacSHA256 = require('crypto-js/hmac-sha256');



class SharedKeyGenerator {
    constructor(storageAccount, tableName, storageKey) {

        this.storageAccountName = storageAccount;
        this.storageAccountKey = storageKey;
        this.tableName = tableName;

    }


    /**
     * generates a shared key lite for authorization
     *
     * @param path path of the resource
     * @returns {string} signed signature
     */
    GenerateSharedKeyLite(path) {
        const date = new Date().toUTCString();

        let stringToSign = date + '\n' + this._getCanonicalizedResource(path);
        const hash = hmacSHA256(Utf8.parse(stringToSign), Base64.parse(this.storageAccountKey));
        const signature = Base64.stringify(hash);

        console.log(stringToSign);
        console.log('SharedKeyLite ' + this.storageAccountName + ':' + signature);
        return 'SharedKeyLite ' + this.storageAccountName + ':' + signature;
    }

    _getCanonicalizedResource(path) {
        return `/${this.storageAccountName}/${path}`;
    }
}

module.exports = SharedKeyGenerator;




这是我执行PUT请求时得到的

stringToSign:
2019年11月21日星期四15:48:07 GMT
/ igdevharuntest / powerbiTableStorage(PartitionKey = NewPartitionKey12,RowKey = CompletelyNewKey12)

签名:
SharedKeyLite igdevharuntest:dt14aw / McZet9JDiZDxnrZnwfMm8AfZZ7jNnTjVJ71A =

{
  'odata.error': {
    code: 'AuthenticationFailed',
    message: {
      lang: 'en-US',
      value: 'Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\n' +
        'RequestId:a9f6868a-c002-001b-6c83-a01440000000\n' +
        'Time:2019-11-21T15:48:07.3247287Z'
    }
  }
}


----更新-----

所以我注意到,当我用括号中的PartitionKey和RowKey值进行GET时,会收到AuthenticationFailed错误。

例如:
uri = GET:https:// {azure_storage_account} .table.core.windows.net / TableStorageName(PartitionKey ='PartitionKey',RowKey ='RowKey')

回应是

{
  'odata.error': {
    code: 'AuthenticationFailed',
    message: {
      lang: 'en-US',
      value: 'Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\n' +
        'RequestId:a9f6868a-c002-001b-6c83-a01440000000\n' +
        'Time:2019-11-21T15:48:07.3247287Z'
    }
  }
}


如果我将括号保留为空,但将过滤器添加为查询参数,则此方法很好,并且可以获取表的特定实体。

例如:
GET:https:// {azure_storage_account} .table.core.windows.net / TableStorageNam()?$ filter = PartitionKey eq'PartitionKey'和RowKey eq'RowKey'

响应是:

{
  value: [
    {
      PartitionKey: 'PartitionKey',
      RowKey: 'RowKey',
      Timestamp: '2019-11-20T18:16:24.5881171Z',
      expiresOn: '2019-10-16T17:27:36.046Z',
      accessToken: 'abcsd123456'
    }
  ]
}


我在想我构建规范化资源的方式可能对括号内的值不正确吗?我不知道..我现在被卡住了。我所有的标头值都是正确的,所以我认为这不是问题。

最佳答案

更新:

这是由于在调用更新/删除功能时缺少partitionKey和RowKey之间的空格。

调用删除功能时,应更改

`deleteEntity("powerbiTableStorage(PartitionKey='ABCDEFG1234567RANDOMPARTITIONKEY1111',RowKey='ABCDEFG1234567')", "");`




`deleteEntity("powerbiTableStorage(PartitionKey='ABCDEFG1234567RANDOMPARTITIONKEY1111', RowKey='ABCDEFG1234567')", "");`


调用更新功能时,应更改

updateEntity("powerbiTableStorage(PartitionKey='ABCDEFG1234567RANDOMPARTITIONKEY1111',RowKey='ABCDEFG1234567')", "?$filter=PartitionKey%20eq%20'ABCDEFG1234567RANDOMPARTITIONKEY1111'%20and%20RowKey%20eq%20'ABCDEFG1234567'");




updateEntity("powerbiTableStorage(PartitionKey='ABCDEFG1234567RANDOMPARTITIONKEY1111', RowKey='ABCDEFG1234567')", "");




对于细节:

const request = require("request");
const crypto = require("crypto");
const url = require('url')

var accountName = "xxx";
var accountKey = "xxx";
var tableName = "test22";
var pk = "r1";
var rk = "s5";

const encodedUriPath = tableName + '(PartitionKey=' + '\'' + pk + '\'' + ', ' + 'RowKey=' + '\'' + rk + '\'' + ')';
console.log(encodedUriPath)
const endpoint = "https://" + accountName + ".table.core.windows.net/" + encodedUriPath;
const parsedUrl = url.parse(endpoint);
const timestamp = (new Date()).toUTCString();

console.log(url);
console.log(timestamp);
const stringToSign = timestamp + '\n/' +  accountName + parsedUrl.path;
console.log('--------------------------------------');
console.log(stringToSign);

const hmac = crypto.createHmac('sha256', new Buffer(accountKey, 'base64'))
                  .update(stringToSign, 'utf-8')
                  .digest('base64');
console.log('--------------------------------------');
console.log(hmac);

request.delete({
  'headers': {
    'Authorization': 'SharedKeyLite ' + accountName + ':' + hmac,
    'x-ms-date': timestamp,
    'x-ms-version': '2016-05-31',
    'Content-Type': 'application/json',
    'If-Match': '*'
  },
  'url': endpoint,
  'json': true
  }, function (err, result) {
  if (err) {
    console.log('inside delete err', JSON.stringify(err));

  } else {
      console.log(JSON.stringify(result));
  }
});


测试结果:

javascript - 如何在NodeJS中对Azure表存储进行PUT或DELETE?-LMLPHP



对于Update EntityDelete Entity rest api,标头If-Match是必需的(请参阅Request Headers)。否则,它将引发错误“ AuthenticationFailed,xxxx”。

在您的updateEntity方法中,在标题部分添加'If-Match': '*',例如:

const response = await client.put(`/${tableName}(PartitionKey='${partitionKey}', RowKey='${rowKey}')`, payload,{
            headers: {
                'x-ms-date': new Date().toUTCString(),
                'Authorization': sharedKey.GenerateSharedKeyLite(tableName),
                Accept: 'application/json;odata=nometadata',
                'x-ms-version': '2015-12-11',
                'Content-Type': 'application/json',
                'If-Match': '*'
            }

09-07 08:45