2

Getting the below error while making a call to Create Container.

Response Code : 411 Response Message : Length Required

String stringToSign = "PUT\n\n\n\n0\n\n\n\n\n\n\n\nx-ms-date:" + date + "\nx-ms-version:" + "2014-02-14\n" + "/" + storageAccount + "/"+ "container-create-test"+"\nrestype:container"+"\ntimeout:60";

Java code snippet.

HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
connection.setRequestMethod(vMethod);
connection.addRequestProperty("Authorization", authHeader);
connection.addRequestProperty("x-ms-date", date);
connection.addRequestProperty("x-ms-version", "2014-02-14");
connection.addRequestProperty("Content-Length", "0");
Jerry Liu
  • 17,282
  • 4
  • 40
  • 61
TechPassionate
  • 1,547
  • 1
  • 11
  • 18

1 Answers1

2

Nothing wrong with the format of StringToSign.

411 Response Message : Length Required

This error means you don't add Content-Length:0 header in your http request.

Update

As you work with HttpURLConnection in Java, Content-Length header can't be set manually by default, see this thread.

In case of other trouble, here's the complete sample for you to refer.

public static void putContainer() throws Exception {
    // Account info
    String accountName = "accountName";
    String accountKey = "accountKey";

    // Request Uri and Method
    String containerName = "containerName";
    String requestUri = "https://"+accountName+".blob.core.windows.net/"+containerName+"?restype=container&timeout=60";
    HttpURLConnection connection = (HttpURLConnection) (new URL(requestUri)).openConnection();
    connection.setRequestMethod("PUT");

    // Request Headers
    // 1. x-ms-version, recommend to use the latest version if possible
    String serviceVersion = "2018-03-28";
    // 2. x-ms-date
    SimpleDateFormat fmt = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
    fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
    String date = fmt.format(Calendar.getInstance().getTime()) + " GMT";
    // 3. Authorization
    String authKeyFormat = "SharedKey";
    String caHeader = "x-ms-date:"+date+"\nx-ms-version:"+serviceVersion+"\n";
    String caResource = "/"+accountName+"/"+containerName+"\nrestype:container\ntimeout:60";
    String signStr = "PUT\n\n\n\n\n\n\n\n\n\n\n\n"+caHeader+caResource;
    String authorization = getAuthorization(accountName, authKeyFormat, signStr, accountKey);

    // Send request
    connection.setRequestProperty("x-ms-version", serviceVersion);
    connection.setRequestProperty("x-ms-date",date);
    connection.setRequestProperty("Authorization", authorization);
    // Send 0 byte, code sets Content-Length:0 automatically
    connection.setDoOutput(true);
    connection.setFixedLengthStreamingMode(0);

    System.out.println("Response message : " + connection.getResponseMessage());
    System.out.println("Response code : " + connection.getResponseCode());
}

private static String getAuthorization(String accountName, String authKeyFormat, String signStr, String accountKey) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {

    SecretKeySpec secretKey = new SecretKeySpec(Base64.getDecoder().decode(accountKey), "HmacSHA256");
    Mac sha256HMAC = Mac.getInstance("HmacSHA256");
    sha256HMAC.init(secretKey);
    String signature = Base64.getEncoder().encodeToString(sha256HMAC.doFinal(signStr.getBytes("UTF-8")));

    return authKeyFormat+" "+accountName+":"+signature;
}
Jerry Liu
  • 17,282
  • 4
  • 40
  • 61
  • @TechPassionate Have added the code sample(including your former question), you could have a try. If you are still baffled, feel free to ask. If those samples do work for you, you could accept them as answers for others to refer. Besides, if there's no specific requirement, [storage sdk](https://github.com/Azure/azure-storage-java) is always preferred. – Jerry Liu Aug 30 '18 at 04:18
  • I see an issue with Create Lease: https://stackoverflow.com/questions/52100823/azure-storage-service-rest-apis-create-lease – TechPassionate Aug 30 '18 at 15:39
  • @JerryLiu Thank you very much! I added the code `connection.setDoOutput(true); connection.setFixedLengthStreamingMode(0);`. It worked! – Deividson Damasio Jun 23 '19 at 18:30