Blob storage REST C#, 400 Bad Request RRS feed

  • Question

  • I am trying to communicate with Azure Blob Storage Container using REST. Cannot use the SDK, restriction in place hence REST is the only option. My code throws a 400 Bad Request exception.

    I have found two examples of the same online.

    Azure Blob Storage Part 5

    and this one

    Azure rest api put blob

    This is my whole code which is a duplicate copy of the two links that I have mentioned

    class Program
        static void Main(string[] args)
        public static void UploadBlobWithRestAPI()
            string storageKey = "ffFJwPXTqyYvRoubNQEti/aQUUMwn41BG3KDtl/yGpG4DR1eKaHRq6Bhbw==";
            string storageAccount = "xyz";
            string containerName = "notes";
            string blobName = "test567";
            string method = "PUT";
            string sampleContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla id euismod urna. Maecenas scelerisque dolor purus, sed ullamcorper ipsum lobortis non. Nulla est justo, sodales at venenatis a, faucibus";
            int contentLength = Encoding.UTF8.GetByteCount(sampleContent);
            string requestUri = $"https://xyz.blob.core.windows.net/notes/test567";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
            string now = DateTime.UtcNow.ToString("R");
            request.Method = method;
            request.ContentType = "text/plain; charset=UTF-8";
            request.ContentLength = contentLength;
            request.Headers.Add("x-ms-version", "2018-01-11");
            request.Headers.Add("x-ms-date", now);
            request.Headers.Add("x-ms-blob-type", "BlockBlob");
            request.Headers.Add("Authorization", AuthorizationHeader2(method, now, request, storageAccount, storageKey, containerName, blobName));
            using (Stream requestStream = request.GetRequestStream())
                requestStream.Write(Encoding.UTF8.GetBytes(sampleContent), 0, contentLength);
            using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
        public static string AuthorizationHeader2(string method, string now, HttpWebRequest request, string storageAccount, 
            string storageKey, string containerName, string blobName)
            string headerResource = $"x-ms-blob-type:BlockBlob\nx-ms-date:"+ DateTime.UtcNow.ToString("R") +"\nx-ms-version:2018-01-11";
            string urlResource = "/xyz/notes/test567";
            string stringToSign =  method + "\n\n\n" + request.ContentLength + 
                "\n\n" + request.ContentType +"\n\n\n\n\n\n\n" + headerResource + "\n" + urlResource;
            HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(storageKey));
            string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
            String AuthorizationHeader = String.Format("{0} {1}:{2}", "SharedKey", storageAccount, signature);
            return AuthorizationHeader;
    Has anybody else faced this issue and solved it, if this is not the correct forum, please do let me know and I will delete my question.

    • Edited by vblord Thursday, January 11, 2018 9:38 AM HTML issues
    • Moved by CoolDadTx Thursday, January 11, 2018 2:48 PM Azure related
    Thursday, January 11, 2018 9:36 AM

All replies