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

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

问题描述

我知道如何列出容器中的所有blob,但我也需要知道blob的类型.现在,我盲目地使用类CloudBlockBlob,因此我得到一个错误,原因是(com.microsoft.azure.storage.StorageException:错误的Blob类型,请使用正确的Blob类型访问服务器上的Blob.期望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天全站免登陆