【问题标题】:Azure Storage Service REST APIs: Create ContainerAzure 存储服务 REST API:创建容器
【发布时间】:2019-02-03 08:23:24
【问题描述】:

调用 Create Container 时出现以下错误。

响应代码:411 响应消息:需要长度

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 代码 sn-p.

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

【问题讨论】:

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


    【解决方案1】:

    StringToSign 的格式没有错。

    411 响应消息:需要长度

    此错误表示您未在 http 请求中添加 Content-Length:0 标头。

    更新

    当您在 Java 中使用 HttpURLConnection 时,默认情况下无法手动设置 Content-Length 标头,请参阅 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;
    }
    

    【讨论】:

    • @TechPassionate 已经添加了代码示例(包括你之前的问题),你可以试试。如果您仍然感到困惑,请随时询问。如果这些示例对您有用,您可以接受它们作为其他人参考的答案。此外,如果没有具体要求,storage sdk 总是首选。
    • 我发现 Create Lease 存在问题:stackoverflow.com/questions/52100823/…
    • @JerryLiu 非常感谢!我添加了代码connection.setDoOutput(true); connection.setFixedLengthStreamingMode(0);。成功了!
    猜你喜欢
    • 1970-01-01
    • 2011-11-26
    • 2017-10-23
    • 1970-01-01
    • 2016-12-15
    • 2018-02-09
    • 1970-01-01
    • 2022-01-22
    • 2021-04-28
    相关资源
    最近更新 更多