【发布时间】: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