Arjun Chaudhary
Arjun Chaudhary

Reputation: 2453

Implementing Put Block of Azure rest api?

I am following this MSDN Reference (https://learn.microsoft.com/en-us/rest/api/storageservices/fileservices/put-block) to implement a rest call for Put Block.

I am coding in Java and I formed below Authorisation string and URL before signing.

PUT


364070








x-ms-blob-type:BlockBlob
x-ms-date:Fri, 20 Jan 2017 12:57:06 GMT
x-ms-version:2016-05-31
/xyz/mycontainer/imageBlock1
comp:block
sun.net.www.protocol.https.DelegateHttpsURLConnection:https://xyz.blob.core.windows.net/mycontainer/imageBlock1?comp=block&blockid=YmxvY2stMQ==

Error I am getting:
403
    Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

I read gaurav mantras post http://gauravmantri.com/2013/02/16/uploading-large-files-in-windows-azure-blob-storage-using-shared-access-signature-html-and-javascript/. But, its not working for me.

Is there anything wrong with the string I am sending to sign or URL or below httpConn Request Header.

The Http Header I am setting is:

httpConn.setRequestMethod("PUT");
                httpConn.setRequestProperty("x-ms-blob-type", blobType);
                httpConn.setRequestProperty("x-ms-date", date);
                httpConn.setRequestProperty("x-ms-version", storageServiceVersion);
                httpConn.setRequestProperty("Authorization", authorizationHeader);
                httpConn.setRequestProperty("Content-Length",String.valueOf(blobLength) );

                System.out.println(httpConn);

                DataOutputStream wr = new DataOutputStream(httpConn.getOutputStream());
                wr.write(bytes);
                wr.flush();
                wr.close();
                int response = httpConn.getResponseCode();

Upvotes: 0

Views: 2368

Answers (1)

Bruce Chen
Bruce Chen

Reputation: 18465

As I known, Put Block is a operation against Block Blobs. So, we do not need to specify x-ms-blob-type header. If you specify this in your HTTP header, you need to follow the following tutorial about constructing the Canonicalized Headers String:

  • Retrieve all headers for the resource that begin with x-ms-, including the x-ms-date header.

  • Convert each HTTP header name to lowercase.

  • Sort the headers lexicographically by header name, in ascending order. Each header may appear only once in the string.

  • Finally, append a new-line character to each canonicalized header in the resulting list. Construct the CanonicalizedHeaders string by concatenating all headers in this list into a single string.

So, based on your code, your canonicalized headers string looks like:

x-ms-blob-type:BlockBlob\nx-ms-date:Fri, 20 Jan 2017 12:57:06 GMT\nx-ms-version:2016-05-31\n

Moreover, the CanonicalizedResource you built is incorrect. Based on your code, it should look as follows:

/{account-name}/{container-name}/{blob-name}\nblockid:{block-id}\ncomp:block

Note: For more details about constructing the Canonicalized Resource String, you could refer to this official document.

The StringToSign would look like this:

And the traffic captured by Fiddler as follows:

Upvotes: 2

Related Questions