none
Azure: delete Table using Azure REST API do not work when done as per documentation

    Întrebare

  • I am following the Azure REST documentation for Table Storage: Delete TableCreate TableAuthentication for the Azure Storage Services. I am able to create the table only after dropping the "Content-Length" header which surprisingly is marked as required and including the "x-ms-version". This I could achieve after a few trial aand error for including the headers.

    Similar issue I am facing for Delete. I am not able to delete the table using REST when strictly following the documentation. I tried a few trial and error but it did not help in delete case.

    Below is the code snippet for create and delete table.

    //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();
        // construct input value
        let inputvalue = `POST\n\napplication/json\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(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",
                "Date": nowUTC
            },
            data: {
                "TableName": "fourtwo"
            }
        };
        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();
        // construct input value
        let inputvalue = `DELETE\n\napplication/json\n${nowUTC}\n/${yourStorageAccountName}/Tables('mytab')`;
        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=minimalmetadata",
                "x-ms-version": "2015-12-11",
                "x-ms-date": nowUTC
            }
        };
        let restClient = new Client();
        restClient.delete(`https://${yourStorageAccountName}.table.core.windows.net/Tables('mytab')`, args, function (data, response) {
            console.log(data);
            //console.log(response);
        });
    }
    
    createTable()
    //deleteTable()

    Same behaviour applies for Get Table ACL as well.

    Am I missing something in both the cases? I am okay with workaround I am using for create. Is there any workaround possible for Delete and Get Table ACL as well?

    Attaching below the screen shot of delete request via Postman rest-client of different combination of headers. 

    Delete Table via Rest client.

    Delete Table via Rest client with different header combination.

    Delete Table via Rest client with different header combination.

    In above Rest calls I am using the signature calculated in the code snippet.


    miercuri, 16 mai 2018 03:49

Răspunsuri

Toate mesajele

  • Use to below code to generate signature then try to delete table.

    string StorageAccount = "account name here";
    string StorageKey = "account key here";
    string tablename = "table name";
    
    string requestMethod = "DELETE";
    string mxdate = "";
    string storageServiceVersion = "2015-12-11";
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(string.Format(CultureInfo.InvariantCulture,
        "https://{0}.table.core.windows.net/Tables('{1}')",
        StorageAccount, tablename));
    
        req.Method = requestMethod;
    
        //specify request header
        string AuthorizationHeader = generateAuthorizationHeader();
        req.Headers.Add("Authorization", AuthorizationHeader);
        req.Headers.Add("x-ms-date", mxdate);
        req.Headers.Add("x-ms-version", storageServiceVersion);
        req.ContentType = "application/json";
    
        req.Accept = "application/json;odata=minimalmetadata";
    
        using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
        {
    
        }
    }
    
    public string generateAuthorizationHeader()
    {
        mxdate = DateTime.UtcNow.ToString("R");
    
        string canonicalizedResource = $"/{StorageAccount}/Tables('{tablename}')"; 
    
        string contentType = "application/json";
    
        string stringToSign = $"{requestMethod}\n\n{contentType}\n{mxdate}\n{canonicalizedResource}";  
    
        HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(StorageKey));
    
        string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
    
        String authorization = String.Format("{0} {1}:{2}",
            "SharedKey",
            StorageAccount,
            signature
            );
    
        return authorization;
    }
    

    miercuri, 16 mai 2018 05:32
    Moderator
  • Issue was fixed by changing the stringToSign key to,  `DELETE\n\n${contentType}\n${nowUTC}\n/${yourStorageAccountName}/Tables(%27fourtwo%27)`;
    joi, 17 mai 2018 13:47
  • Glad to know that issue has been resolved.

    duminică, 20 mai 2018 02:23
    Moderator