微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Azure blob Java sdk 无法返回对象的 versionID 列表

如何解决Azure blob Java sdk 无法返回对象的 versionID 列表

我正在尝试通过 Java SDK 12.10.2 版获取 Azure blob 的 versionID 列表,但它只返回 HTTP 错误 400 并带有以下错误消息

com.azure.storage.blob.models.BlobStorageException: Status code 400,"<?xml version="1.0" encoding="utf-8"?><Error><Code>InvalidQueryParameterValue</Code><Message>Value for one of the query parameters specified in the request URI is invalid.
RequestId:bd7e9a20-401e-003e-6d7b-37d0dd000000
Time:2021-04-22T13:29:12.0965760Z</Message><QueryParameterName>include</QueryParameterName><QueryParameterValue>versions</QueryParameterValue><Reason>Invalid query parameter value.</Reason></Error>"

而且,这是我的 Java 代码

public static BlobContainerAsyncclient getAzureAsyncclient(String accountName,String accountKey,String endpoint) {
        String connectionString = getConnectionString(accountName,accountKey,endpoint);
        BlobContainerAsyncclient blobServiceClient = new BlobContainerClientBuilder()
          .connectionString(connectionString)
          .serviceVersion(version)
                  .buildAsyncclient();
    RWLog.MAIN.info("Azure Storage Async Service Client created for " + accountName);    
        return blobServiceClient;
}

BlobContainerAsyncclient containerClient = AzureConnection.getAzureAsyncclient(folder.getBucket(),folder.getSecretKey(),folder.getEndpoint());
    ListBlobsOptions ops = new ListBlobsOptions();
    ops.getDetails().setRetrieveVersions(true);
    ops.setPrefix(blobKey);
    PagedFlux<BlobItem> list =
        containerClient.listBlobsByHierarchy("/",ops);

看起来 Azure blob 服务器无法识别 Java SDK 构建的 HTTP 请求。服务器端无法处理来自 Java 客户端的包含版本的查询。 有什么想法吗?

解决方法

只需使用 azure-storage-blob 的 12.10.2 尝试此操作即可获取 blob 的所有版本:

public static void main(String[] args) {

                String connString = "";
                String containerName = "";
                String blobName = "";

                BlobServiceClient client = new BlobServiceClientBuilder().connectionString(connString).buildClient();

                BlobContainerClient blobContainerClient = client.getBlobContainerClient(containerName);
                ListBlobsOptions options = new ListBlobsOptions().setPrefix(blobName)
                                .setDetails(new BlobListDetails().setRetrieveVersions(true));

                Iterator<BlobItem> it = blobContainerClient.listBlobs(options,null).iterator();
                while (it.hasNext()) {
                        BlobItem item = it.next();
                        System.out.println(" versionID: " + item.getVersionId());
                }

        }

结果:

enter image description here enter image description here

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。