【问题标题】:How to determine the Blob type of all the blobs in a container in Azure?如何确定 Azure 容器中所有 Blob 的 Blob 类型?
【发布时间】:2015-12-07 15:36:27
【问题描述】:

我知道如何列出容器中的所有 blob,但我也需要知道 blob 的类型。现在我盲目地使用 CloudBlockBlob 类,因此我收到错误消息(com.microsoft.azure.storage.StorageException: Incorrect Blob type, please use the correct Blob type to access a blob on the server. Expected BLOCK_BLOB,实际的 PAGE_BLOB。)列表中有一种 PageBlob 类型。有没有办法确定 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;
}

【问题讨论】:

    标签: java api sdk azure-storage azure-blob-storage


    【解决方案1】:

    您可以检查 blobItem 的类型。例如,如下所示:

    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 块,但我建议您更新以获得最新的错误修复以及该功能。另请注意,这消除了解析名称的需要。

    【讨论】:

    • 谢谢...我想我们可以用 instanceof 做同样的事情
    【解决方案2】:

    添加另一种检查类型@Emily 答案的方法,您可以使用“is”运算符。

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

    与 cast 相比,这看起来更具可读性和更快 ;)

    【讨论】:

      猜你喜欢
      • 2016-11-24
      • 2019-10-19
      • 2019-01-18
      • 2015-11-10
      • 2020-10-09
      • 2020-01-12
      • 2020-04-09
      • 2019-03-25
      相关资源
      最近更新 更多