title: I have created container successfully but failed to upload an image/pdf to my blob container using Rest API
body: <p>I am using PUT Blob Rest API for uploading img/pdf- <a href="https://docs.microsoft.com/en-us/rest/api/storageservices/put-blob" rel="nofollow noreferrer">PUT Blob reference</a>. But it is showing- Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature error (403 response code).</p>
  <p><strong>Below are the string to sign:</strong></p>
  <p>&quot;PUT\n\n\n41676\n\nimage/png\n\n\n\n\n\n\nx-ms-blob-type:BlockBlob\nx-ms-date:Wed, 17 Feb 2021 10:24:02 GMT\nx-ms-version:2020-02-10\n/acc_name/container_name/12362_50800_13_s_1.png&quot;</p>
  <p><strong>Below are the header details:</strong></p>
  <p>{ &quot;x-ms-blob-type&quot; : &quot;BlockBlob&quot; , &quot;x-ms-date&quot; : &quot;Wed, 17 Feb 2021 10:24:02 GMT&quot; , &quot;x-ms-version&quot; : &quot;2020-02-10&quot; , &quot;Authorization&quot; : &quot;SharedKey acc_name:XXXQsR728D7aqyJzFi/HXk+qT9rpjYbplUG9KXXXX&quot; , &quot;Content-Type&quot; : &quot;image/png&quot; , &quot;Content-Length&quot; : &quot;41676&quot;}</p>
  <p><strong>Below are the URL details:</strong></p>
  <p><a href="https://acc_name.blob.core.windows.net/container_name/12362_50800_13_s_1.png" rel="nofollow noreferrer">https://acc_name.blob.core.windows.net/container_name/12362_50800_13_s_1.png</a></p>
  <pre><code>private BasicDBObject createAzureBlob(String storageAPIVersion, String storageAccURL, String storageAccName, String storageAcckey, String storageAccConString,
                                        String containerName, BasicDBObject clientReqObj, File outputfile) throws Exception
  {
      log.info(&quot;Entry createAzureBlob in the GenericServicesDeligator&quot;);
  
      BasicDBObject clientResponse = null;
  
      String date = clientReqObj.remove(Constants.F_CREATED_DATE).toString();
      String filePath = clientReqObj.remove(Constants.F_PATH).toString();
      String contentLength = clientReqObj.remove(&quot;Content-Length&quot;).toString();
      String contentType = clientReqObj.remove(&quot;Content-Type&quot;).toString();
      
      String hostMethod = &quot;PUT&quot;;
      StringBuilder authorization = new StringBuilder(&quot;SharedKey &quot;).append(storageAccName).append(&quot;:&quot;).append(&quot;sharedKeySign&quot;);
      StringBuilder canonicalizedResource = new StringBuilder(&quot;/&quot;).append(storageAccName).append(&quot;/&quot;).append(containerName).append(&quot;/&quot;).append(filePath);
      
      BasicDBObject clientReqHeader = new BasicDBObject();
  
      // Host URL
      String hostName = clientReqHeader.size()&gt;0l?new StringBuilder(&quot;/&quot;).append(filePath).append(prepareHostInfo(clientReqHeader)).toString():new StringBuilder(&quot;/&quot;).
                        append(filePath).toString();
      String hostURL = new StringBuilder(storageAccURL).append(&quot;/&quot;).append(containerName).append(hostName).toString().trim();
      
      // canonicalizedResource should always be in alphabetical order
      canonicalizedResource.append(prepareCanonicalizedResource(clientReqHeader));
  
      //canonicalizedHeader should always be in alphabetical order and include only that begin with x-ms-
      clientReqHeader.clear();
      clientReqHeader.put(&quot;x-ms-blob-type&quot;, &quot;BlockBlob&quot;);               
      clientReqHeader.put(&quot;x-ms-date&quot;, date);
      clientReqHeader.put(&quot;x-ms-version&quot;, storageAPIVersion);
  
      String canonicalizedHeader = prepareCanonicalizedHeader(clientReqHeader);
  
      // string to sign
      String stringToSign = prepareStringToSign(hostMethod, contentLength, contentType, canonicalizedHeader, canonicalizedResource.toString());
      
      // encode the string by using the HMAC-SHA256 algorithm over the UTF-8-encoded signature string
      String signature = encodeSharedKeySignature(stringToSign, storageAcckey);
  
      clientReqHeader.put(&quot;Authorization&quot;, authorization.toString().replace(&quot;sharedKeySign&quot;, signature));
      clientReqHeader.put(&quot;Content-Length&quot;, contentLength);
      clientReqHeader.put(&quot;Content-Type&quot;, contentType);
      
      clientResponse = sendAzureClientRequest(hostMethod, hostURL, clientReqHeader, clientReqObj, outputfile);
  
      log.info(&quot;Exit createAzureBlob in the GenericServicesDeligator&quot;);
  
      return clientResponse;
  </code></pre>
  <p>}</p>
  <pre><code>private String prepareHostInfo(BasicDBObject hostNameObject) throws Exception
  {
      StringBuilder hostName = new StringBuilder();
      
      for(String key: hostNameObject.keySet())
      {
          if(hostName.length() == 0l)
              hostName.append(&quot;?&quot;).append(key).append(&quot;=&quot;).append(hostNameObject.get(key));
          else
              hostName.append(&quot;&amp;&quot;).append(key).append(&quot;=&quot;).append(hostNameObject.get(key));
      }
      
      return hostName.toString().trim();
  }
  private String prepareCanonicalizedResource(BasicDBObject hostNameObject) throws Exception
  {
      StringBuilder canonicalizedResource = new StringBuilder();
      
      Map&lt;String, String&gt; canonicalizedMap = new TreeMap&lt;String, String&gt;();
      
      canonicalizedMap.putAll(hostNameObject.toMap());
      for(String key: canonicalizedMap.keySet())
      {
          canonicalizedResource.append(&quot;\n&quot;).append(key).append(&quot;:&quot;).append(canonicalizedMap.get(key));
      }
      
      return canonicalizedResource.toString();
  }
  private String prepareCanonicalizedHeader(BasicDBObject hostNameObject) throws Exception
  {
      StringBuilder canonicalizedHeader = new StringBuilder();
      
      Map&lt;String, String&gt; canonicalizedMap = new TreeMap&lt;String, String&gt;();
      
      canonicalizedMap.putAll(hostNameObject.toMap());
      for(String key: canonicalizedMap.keySet())
      {
          canonicalizedHeader.append(key).append(&quot;:&quot;).append(canonicalizedMap.get(key).toString().trim()).append(&quot;\n&quot;);
      }
      
      return canonicalizedHeader.toString();
  }
  private String prepareStringToSign(String hostMethod, String contentLength, String contentType, String canonicalizedHeader, String canonicalizedResource) 
  {
      String input = &quot;&quot;;
      
      input =  new StringBuilder(hostMethod).append(&quot;\n&quot;).
               append(&quot;\n&quot;).                                                           // Content-Encoding + &quot;\n&quot; +
               append(&quot;\n&quot;).                                                           // Content-Language + &quot;\n&quot; +
               append(contentLength!=null?contentLength:&quot;&quot;).append(&quot;\n&quot;).
               append(&quot;\n&quot;).                                                           // Content-MD5 + &quot;\n&quot; +
               append(contentType!=null?contentType:&quot;&quot;).append(&quot;\n&quot;). 
               append(&quot;\n&quot;).                                                           // Date
               append(&quot;\n&quot;).                                                           // If-Modified-Since + &quot;\n&quot; +
               append(&quot;\n&quot;).                                                           // If-Match + &quot;\n&quot; +
               append(&quot;\n&quot;).                                                           // If-None-Match + &quot;\n&quot; +
               append(&quot;\n&quot;).                                                           // If-Unmodified-Since + &quot;\n&quot; +
               append(&quot;\n&quot;).                                                           // Range + &quot;\n&quot; +
               append(canonicalizedHeader).
               append(canonicalizedResource).toString().trim();
      
      return input;
  }
  
  public String getUTCDate(Date date) throws Exception
  {
      SimpleDateFormat sdf = new SimpleDateFormat(&quot;EEE, dd MMM yyyy HH:mm:ss&quot;);
      sdf.setTimeZone(TimeZone.getTimeZone(&quot;GMT&quot;));
     
      return (sdf.format(date) + &quot; GMT&quot;).trim();
  }
  
  public static String getFileContentType(String fileType)
  {
      String contentType = &quot;&quot;;
      switch (fileType)
      {
          case &quot;pdf&quot;:
              contentType = &quot;application/pdf&quot;;
              break;
          case &quot;txt&quot;:
              contentType = &quot;text/plain&quot;;
              break;
          case &quot;bmp&quot;:
              contentType = &quot;image/bmp&quot;;
              break;
          case &quot;gif&quot;:
              contentType = &quot;image/gif&quot;;
              break;
          case &quot;png&quot;:
              contentType = &quot;image/png&quot;;
              break;
          case &quot;jpg&quot;:
              contentType = &quot;image/jpeg&quot;;
              break;
          case &quot;jpeg&quot;:
              contentType = &quot;image/jpeg&quot;;
              break;
          case &quot;xls&quot;:
              contentType = &quot;application/vnd.ms-excel&quot;;
              break;
          case &quot;xlsx&quot;:
              contentType = &quot;application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&quot;;
              break;
          case &quot;csv&quot;:
              contentType = &quot;text/csv&quot;;
              break;
          case &quot;html&quot;:
              contentType = &quot;text/html&quot;;
              break;
          case &quot;xml&quot;:
              contentType = &quot;text/xml&quot;;
              break;
          case &quot;zip&quot;:
              contentType = &quot;application/zip&quot;;
              break;
          default:
              contentType = &quot;application/octet-stream&quot;;
              break;
      }
      return contentType.trim();
  }
  
  
  private String encodeSharedKeySignature(String inputVal, String storageAcckey) throws Exception
  {
       SecretKeySpec secretkey = new SecretKeySpec(new BASE64Decoder().decodeBuffer(storageAcckey), &quot;HmacSHA256&quot;);
       
       Mac mac = Mac.getInstance(&quot;HmacSHA256&quot;);
       mac.init(secretkey);
  
       String hash = Base64.encodeAsString(mac.doFinal(inputVal.getBytes(&quot;UTF-8&quot;)));
       
       return hash.trim();
  }
  </code></pre>
  <p>Kindly share the solution so i would upload a image/pdf.
  Thanks in advance</p>