If you are encountering this exception on CloudBlockBlob.PutBlock method, then you might be one of the unfortunate developer who is trying to figure out what went wrong without much support from the exception object.
Though there are many reasons for the same, one of the common mistake is the encoding we use. Let me detail out the Exception object.
{“Value for one of the query parameters specified in the request URI is invalid.”}
[Microsoft.WindowsAzure.StorageClient.StorageClientException]: {“Value for one of the query parameters specified in the request URI is invalid.”}
_className: null
_data: {System.Collections.ListDictionaryInternal}
_dynamicMethods: null
_exceptionMethod: {T get_Result()}
_exceptionMethodString: null
_helpURL: null
_HResult: -2146233088
_innerException: {“The remote server returned an error: (400) Bad Request.”}
_ipForWatsonBuckets: 8791477506185
_message: “Value for one of the query parameters specified in the request URI is invalid.”
_remoteStackIndex: 0
_remoteStackTraceString: null
_safeSerializationManager: {System.Runtime.Serialization.SafeSerializationManager}
_source: “Microsoft.WindowsAzure.StorageClient”
_stackTrace: {sbyte[160]}
_stackTraceString: null
_watsonBuckets: null
_xcode: -532462766
_xptrs: 0
Data: {System.Collections.ListDictionaryInternal}
HelpLink: null
HResult: -2146233088
InnerException: {“The remote server returned an error: (400) Bad Request.”}
IsTransient: false
Message: “Value for one of the query parameters specified in the request URI is invalid.”
Source: “Microsoft.WindowsAzure.StorageClient”
The code which will raise the above exception might be as follows
blockBlob.PutBlock("Block1", new MemoryStream(buffer), null);
If you are looking at the documentation for this method, it says that the block id is supposed to be of base-64 encoding which we haven’t done. So let us rewrite the code as below.
string blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes("Block1")); blockBlob.PutBlock(blockId, new MemoryStream(buffer), null);
The above code should solve your problem!