java - Azure 存储服务 REST API : Create Container

标签 java rest azure azure-storage azure-blob-storage

调用创建容器时出现以下错误。

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 代码片段。

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");

最佳答案

StringToSign 的格式没有问题。

411 Response Message : Length Required

此错误意味着您没有在 http 请求中添加 Content-Length:0 header 。

更新

当您在 Java 中使用 HttpURLConnection 时,默认情况下无法手动设置 Content-Length header ,请参阅 this thread .

如果遇到其他问题,这里有完整的示例供您引用。

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;
}

关于java - Azure 存储服务 REST API : Create Container,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52062313/

相关文章:

python - Django Rest Framework如何自定义ListAPIView

Angular 应用程序显示 cors 问题并且未重定向到 azure 广告登录页面

java - SwingWorker 更新多面板中的多个组合框

java - jackson 将不同的字符串反序列化为相同的枚举常量

java - Jersey 消耗二维数组

javascript - 在 50 个小块中用 Javascript 执行 10 万个 Promise

azure - 如何将 azure 应用程序洞察链接到 aks

powershell - 更新-AzureRmVmss : Required parameter 'adminPassword' is missing (null)

java - ZuulException 转发错误,ClientException null

java - 使用时钟更新 TextView