簡體   English   中英

Azure:按照文檔完成后,使用Azure REST API刪除表不起作用

[英]Azure: delete Table using Azure REST API do not work when done as per documentation

我正在關注表存儲的Azure REST文檔: 刪除表創建表Azure存儲服務的身份驗證 我只有在刪除“ 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()

同樣的行為也適用於“ 獲取表” ACL

我在這兩種情況下都缺少什么嗎? 我可以使用用於創建的解決方法。 還有刪除和獲取表ACL的解決方法嗎?

通過Postman rest-client附加在刪除請求的屏幕截圖下方。 通過Rest客戶端刪除表。

根據Gaurav Mantri的建議,通過帶有標題的Rest客戶端刪除Table。

通過具有不同標題組合的Rest客戶端刪除表。

在上面的Rest調用中,我使用代碼片段中計算出的簽名。

您的大多數代碼都是正確的,除了一件小事(我很抱歉告訴您刪除內容類型標頭)。 本質上,在您的inputvalue ,資源路徑應使用url編碼。 因此,您的inputvalue應為:

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

%27轉義'即用%27mytab%27替換'mytab'並且不會出現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);
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM