0
votes

I am following this MSDN Reference (https://docs.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();
1
What is 'authorizationHeader'. Have you prefix it with "Bearer: "?yonisha
You have comp:block in your string to sign but not blockid:YmxvY2stMQ==. Also, please ensure that there is no newline at the end of the last query parameter in the string to sign. Finally, always check the response body when you get 403. In case of signature mismatch, the service will return the string to sign that it used and you can compare to your own.Michael Roberson - MSFT
Have you solved this issue, any updates?Bruce Chen

1 Answers

1
votes

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: