gpt4 book ai didi

java - Azure REST API 使用 java 获取存储 Blob 大小详细信息

转载 作者:行者123 更新时间:2023-12-02 04:15:45 25 4
gpt4 key购买 nike

我正在使用 Azure REST API 使用 java 获取存储 Blob 大小详细信息,我收到响应“服务器无法对请求进行身份验证。请确保授权 header 的值格式正确,包括签名”

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

// Request Uri and Method
String containerName = "ContainerName";
String requestUri = "https://" + accountName + ".blob.core.windows.net/" + containerName + "?restype=container&comp=metadata";
System.out.println("requestUri = " + requestUri);
HttpURLConnection connection = (HttpURLConnection) (new URL(requestUri)).openConnection();
connection.setRequestMethod("GET");

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

String authKeyFormat = "SharedKey";
String caHeader = "x-ms-date:" + date + "\nx-ms-version:" + serviceVersion + "\n";
String caResource = "/" + accountName + "/" + containerName + "ncomp:metadata\\nrestype:container";
String signStr = "GET\n\n\n\n\n\n\n\n\n\n\n" + caHeader + caResource;
System.out.println("signStr = " + signStr);
String authorization = getAuthorization(accountName, authKeyFormat, signStr, accountKey);
System.out.println("x-ms-version = " + serviceVersion);
System.out.println("x-ms-date = " + date);
System.out.println("Authorization = " + authorization);
connection.setRequestProperty("x-ms-version", serviceVersion);
connection.setRequestProperty("x-ms-date", date);
connection.setRequestProperty("Authorization", authorization);
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, java.security.InvalidKeyException, Base64DecodingException {
SecretKeySpec secretKey = new SecretKeySpec(Base64.decode(accountKey), "HmacSHA256");
Mac sha256HMAC = Mac.getInstance("HmacSHA256");
sha256HMAC.init(secretKey);
String signature = Base64.encode(sha256HMAC.doFinal(signStr.getBytes("UTF8")));
return authKeyFormat + " " + accountName + ":" + signature;
}

存储请求和响应详细信息

  GET -  https://StorageName.blob.core.windows.net/ContainerName?restype=container&comp=metadata   
x-ms-version = 2018-03-28
x-ms-date = Tue, 18 Jun 2019 13:46:41 GMT
Authorization = SharedKey StorageName:Pp8E/FAxeIHDYs17r2GRYvL8xAgJ/D5eJuqlVW3+aiU=

响应消息:服务器无法验证请求。确保授权 header 的值格式正确,包括签名。响应代码:403

我们无法验证存储帐户,因此我们无法获取 blob 内容大小

最佳答案

我建议您使用 Java sdk 进行 Azure 存储,因为它提供了灵 active 和更多选项。通过使用 SDK,您可以简单地使用 BlobProperties 来获取大小。

您可以引用以下文档:

https://azure.github.io/azure-sdk-for-java/com/microsoft/azure/storage/blob/BlobProperties.html#getLength--

如果您仍打算使用其余 api,那么我建议您查看以下 api:

https://myaccount.blob.core.windows.net/mycontainer/myblob

**获取 Blob** 属性操作返回 Blob 的所有用户定义元数据、标准 HTTP 属性和系统属性。它不返回 blob 的内容

这是示例响应,Content-Length:是您可能感兴趣的标签,因为它给出了以字节为单位的总体大小。

Response Status:  
HTTP/1.1 200 OK

Response Headers:
x-ms-meta-Name: myblob.txt
x-ms-meta-DateUploaded: <date>
x-ms-blob-type: AppendBlob
x-ms-lease-status: unlocked
x-ms-lease-state: available
Content-Length: 11
Content-Type: text/plain; charset=UTF-8
Date: <date>
ETag: "0x8CAE97120C1FF22"
Accept-Ranges: bytes
x-ms-blob-committed–block-count: 1
x-ms-version: 2015-02-21
Last-Modified: <date>
Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
x-ms-copy-id: 36650d67-05c9-4a24-9a7d-a2213e53caf6
x-ms-copy-source: <url>
x-ms-copy-status: success
x-ms-copy-progress: 11/11
x-ms-copy-completion-time: <date>

希望有帮助。

https://learn.microsoft.com/en-us/rest/api/storageservices/get-blob-properties

关于java - Azure REST API 使用 java 获取存储 Blob 大小详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56660754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com