gpt4 book ai didi

java - 如何确定Azure中容器中所有Blob的Blob类型?

转载 作者:行者123 更新时间:2023-12-02 04:23:13 24 4
gpt4 key购买 nike

我知道如何列出容器中的所有 blob,但我也需要知道 blob 的类型。现在我盲目地使用 CloudBlockBlob 类,因为我收到一个错误(com.microsoft.azure.storage.StorageException:不正确的 Blob 类型,请使用正确的 Blob 类型访问服务器上的 blob。预期的 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;
}

最佳答案

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

关于java - 如何确定Azure中容器中所有Blob的Blob类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32517290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com