Azure:使用Azure REST API删除表在按文档完成时不起作用

时间:2018-05-15 19:03:19

标签: node.js azure azure-storage azure-table-storage

我正在关注表存储的Azure REST文档:Delete TableCreate TableAuthentication for the Azure Storage Services。我只能在删除" Content-Length"之后才能创建表。令人惊讶地标记为必需的标题,包括" x-ms-version"。这可以通过几次试验和包含标题的错误后实现。

我面临的类似问题是删除。严格遵循文档时,我无法使用REST删除表。我尝试了一些试验和错误,但它在删除案例中没有帮助。

以下是创建和删除表的代码段。

//Input your Storage Account and access-key associated to it.
const yourStorageAccountName = '';
const accessKeyStorageAccount = '';
const Client = require('node-rest-client').Client;
const crypto = require("crypto");

async function createTable() {
    let now = new Date();
    let nowUTC = now.toUTCString();
    let contentType = "application/json";
    // construct input value
    let stringToSign = `POST\n\n${contentType}\n${nowUTC}\n/${yourStorageAccountName}/Tables`;
    let accesskey = accessKeyStorageAccount;
    // create base64 encoded signature
    let key = new Buffer(accesskey, "base64");
    let hmac = crypto.createHmac("sha256", key);
    hmac.update(stringToSign);
    let sig = hmac.digest("base64");
    console.log("SIGNATURE : " + sig);
    let args = {
        headers: {
            "Authorization": "SharedKey " + yourStorageAccountName + ":" + sig,
            "Content-Type": contentType,
            "Accept": "application/json;odata=nometadata",
            "x-ms-version": "2015-12-11",
            "Date": nowUTC,
            "DataServiceVersion": '3.0',
            "MaxDataServiceVersion": '3.0'
        },
        data: {
            "TableName": "fortwo"
        }
    };
    let restClient = new Client();
    restClient.post(`https://${yourStorageAccountName}.table.core.windows.net/Tables`, args, function (data, response) {
        console.log(data);
        //console.log(response);
    });

}

async function deleteTable() {
    let now = new Date();
    let nowUTC = now.toUTCString();
    let contentType = "application/json"
    // construct input value
    let stringToSign = `DELETE\n\n${contentType}\n${nowUTC}\n/${yourStorageAccountName}/Tables(%27fourtwo%27)`;
    let accesskey = accessKeyStorageAccount;
    // create base64 encoded signature
    let key = new Buffer(accesskey, "base64");
    let hmac = crypto.createHmac("sha256", key);
    hmac.update(stringToSign);
    let sig = hmac.digest("base64");
    console.log("SIGNATURE : " + sig);
    console.log("nowutc : " + nowUTC);
    let args = {
        headers: {
            "Authorization": "SharedKey " + yourStorageAccountName + ":" + sig,
            "Content-Type": contentType,
            "Accept": "application/json;odata=nometadata",
            "Date": nowUTC,
            "x-ms-version": "2015-12-11",
            "DataServiceVersion": '3.0',
            "MaxDataServiceVersion": '3.0'
        }
    };
    let restClient = new Client();
    restClient.delete(`https://${yourStorageAccountName}.table.core.windows.net/Tables('fourtwo')`, args, function (data, response) {
        console.log(data);
        //console.log(response);
    });
}

async function getTableAcl() {
    let now = new Date();
    let nowUTC = now.toUTCString();
    let contentType = "application/json"
    // construct input value
    let stringToSign = `GET\n\n\n${nowUTC}\n/${yourStorageAccountName}/goodwa\ncomp:acl`;
    let accesskey = accessKeyStorageAccount;
    // create base64 encoded signature
    let key = new Buffer(accesskey, "base64");
    let hmac = crypto.createHmac("sha256", key);
    hmac.update(stringToSign);
    let sig = hmac.digest("base64");
    console.log("SIGNATURE : " + sig);
    console.log("nowutc : " + nowUTC);
    let args = {
        headers: {
            "Authorization": "SharedKey " + yourStorageAccountName + ":" + sig,
            "Date": nowUTC,
            "x-ms-version": "2015-12-11"
        }
    };
    let restClient = new Client();
    restClient.get(`https://${yourStorageAccountName}.table.core.windows.net/goodwa?comp=acl`, args, function (data, response) {
        console.log(JSON.stringify(data));
        //console.log(response);
    });
}


//createTable()
//deleteTable()
getTableAcl()

同样的行为也适用于Get Table ACL

我在两个案件中都遗漏了什么? 我可以使用我用于创建的解决方法。删除和Get Table ACL是否有可能的解决方法?

通过Postman rest-client附加删除请求的屏幕截图。 Delete Table via Rest client.

Delete Table via Rest client with headers as suggested by Gaurav Mantri.

Delete Table via Rest client with different header combination.

在上面的Rest调用中,我使用的是代码片段中计算的签名。

1 个答案:

答案 0 :(得分:2)

除了一件小事之外,你的大部分代码都是正确的(我很抱歉告诉你删除内容类型标题)。基本上,在inputvalue中,资源路径应该是url编码的。所以你的inputvalue应该是:

let inputvalue = `DELETE\n\napplication/json\n${nowUTC}\n/${yourStorageAccountName}/Tables(%27mytab%27)`;

使用'转义%27,将'mytab'替换为%27mytab%27,您不应该收到403错误。

以下是我使用的代码:

function deleteTable() {
    let now = new Date();
    let nowUTC = now.toUTCString();
    // construct input value
    let inputvalue = `DELETE\n\napplication/json\n${nowUTC}\n/${yourStorageAccountName}/Tables(%27mytab%27)`;
    console.log('inputvalue');
    console.log(inputvalue)
    let accesskey = accessKeyStorageAccount;
    // create base64 encoded signature
    let key = new Buffer(accesskey, "base64");
    let hmac = crypto.createHmac("sha256", key);
    hmac.update(inputvalue);
    let sig = hmac.digest("base64");
    console.log("SIGNATURE : " + sig);
    let args = {
        headers: {
            "Authorization": "SharedKey " + yourStorageAccountName + ":" + sig,
            "Content-Type": "application/json",
            "Accept": "application/json;odata=nometadata",
            "x-ms-version": "2015-12-11",
            "x-ms-date": nowUTC,
            "DataServiceVersion": '3.0',
            "MaxDataServiceVersion": '3.0'
        }
    };
    let restClient = new Client();
    restClient.delete(`https://${yourStorageAccountName}.table.core.windows.net/Tables('mytab')`, args, function (data, response) {
        console.log(data);
        console.log(response.statusCode);
    });
}