如何确定 Azure 中容器中所有 Blob 的 Blob 类型? [英] How to determine the Blob type of all the blobs in a container in Azure?

查看:32
本文介绍了如何确定 Azure 中容器中所有 Blob 的 Blob 类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何列出容器中的所有 blob,但我也需要知道 blob 的类型.现在我盲目地使用 CloudBlockBlob 类,因为我收到一个错误 (com.microsoft.azure.storage.StorageException: Incorrect Blob type, please use the right Blob type to access a blob on the server. Expected BLOCK_BLOB,实际的 PAGE_BLOB.)列表中有一种 PageBlob 类型.有没有办法确定 Blob 的类型?

I know how to list all the blobs in a container but I need to know the type of blob too. Right now I am blindly using the class CloudBlockBlob because of which I am getting an error as (com.microsoft.azure.storage.StorageException: Incorrect Blob type, please use the correct Blob type to access a blob on the server. Expected BLOCK_BLOB, actual PAGE_BLOB.) there's one PageBlob type in the list. Is there a way to determine the type of the Blob?

这是我的代码的样子:

public static void getContainerFiles(CloudStorageAccount storageAccount, String containerName) {
    String fName = "";
    Date lstMdfdDate = null;
    try
    {
        // Define the connection-string with your values
        String storageConnectionString = "DefaultEndpointsProtocol=https;" +"AccountName=" + storageName + ";AccountKey="+key;
        // Retrieve storage account from connection-string.
        storageAccount = CloudStorageAccount.parse(storageConnectionString);
        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

        // Retrieve reference to a previously created container.
        CloudBlobContainer container = blobClient.getContainerReference(containerName);

        StorageCredentials cred = storageAccount.getCredentials();
        System.out.println(">>> AccountName: " + cred.getAccountName());
        System.out.println("Listing files of \"" + containerName + "\" container");
        // Loop over template directory within the container to get the template file names.
        CloudBlockBlob blob = null; 
        for (ListBlobItem blobItem : container.listBlobs()) {
          fName = getFileNameFromBlobURI(blobItem.getUri(), containerName);
            blob = container.getBlockBlobReference(fName);
            blob.downloadAttributes();
            lstMdfdDate = blob.getProperties().getLastModified();
        }
    } catch (Exception e) {
    }
}

private static String getFileNameFromBlobURI(URI uri, String containerName)
{
    String urlStr = uri.toString();
    String keyword = "/"+containerName+"/";
    int index = urlStr.indexOf(keyword) + keyword.length();
    String filePath = urlStr.substring(index);
    return filePath;
}

推荐答案

您可以检查 blobItem 的类型.例如,类似于以下内容:

You can check the type of the blobItem. For example, something like the following:

if (CloudBlockBlob.class == blobItem.getClass()) {
    blob = (CloudBlockBlob)blobItem;
}
else if (CloudPageBlob.class == blobItem.getClass()) {
    blob = (CloudPageBlob)blobItem;
}
else if (CloudAppendBlob.class == blobItem.getClass()) {
    blob = (CloudAppendBlob)blobItem;
}

如果您使用的是旧版本的库,您可以省略 CloudAppendBlob 块,但我建议您更新以获得最新的错误修复以及该功能.另请注意,这消除了解析名称的需要.

You can omit the CloudAppendBlob block if you're on older versions of the library, but I'd recommend updating to get the latest bug fixes as well as that feature. Also notice that this removes the need to parse the name out.

这篇关于如何确定 Azure 中容器中所有 Blob 的 Blob 类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆