简体   繁体   中英

How to determine the Blob type of all the blobs in a container in Azure?

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?

This is how my code looks like:

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;
}

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;
}

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.

Adding just another way to check type @Emily's answer, you can just use "is" operator.

if (blobItem is CloudBlockBlob)
{
 ...
}
else if (blobItem is CloudPageBlob)
{
 ...
}
else if (blobItem is CloudAppendBlob)
{
 ...
}

This looks more readable and faster compared to cast ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM