当我尝试迭代 ListBlobs() 调用的结果时,出现“指定的资源不存在”异常。当我直接访问它时,我可以获得 blob 属性,但我试图获取子目录中所有 blob 的列表。
我写了这个小测试来看看问题到底出在哪里。我在这里有一个测试驱动程序和两种方法。第一种方法“GetBlockBlobDateTime”运行良好并返回现有 blob 的日期时间。第二种方法“GetBlobDirFiles”使用相同的输入并在我尝试迭代 blobItems 时抛出异常。
foreach (IListBlobItem blobItem in blobItems)
请注意,两种方法都使用相同的数据。我错过了什么?
public static void DoTest(string baseURL, string container, string directory, string fileName)
{
DateTime t = GetBlockBlobDateTime( baseURL, container, directory, fileName);
List<string> fileList = GetBlobDirFiles( baseURL, container, directory);
}
public static DateTime GetBlockBlobDateTime(string baseURL, string container, string directory, string fileName)
{
CloudBlobClient blobClient = new CloudBlobClient(baseURL);
CloudBlobDirectory blobDir = blobClient.GetBlobDirectoryReference(container);
CloudBlobDirectory subDirectory = blobDir.GetSubdirectory(directory);
CloudBlockBlob cloudBlockBlob = subDirectory.GetBlockBlobReference(fileName);
cloudBlockBlob.FetchAttributes();
DateTime cloudTimeStampUTC = cloudBlockBlob.Properties.LastModifiedUtc;
return cloudTimeStampUTC;
}
public static List<string> GetBlobDirFiles(string baseURL, string container, string directory)
{
CloudBlobClient blobClient = new CloudBlobClient(baseURL);
CloudBlobDirectory blobDir = blobClient.GetBlobDirectoryReference(container);
CloudBlobDirectory subDirectory = blobDir.GetSubdirectory(directory);
IEnumerable<IListBlobItem> blobItems = subDirectory.ListBlobs();
List<string> fileList = new List<string>();
foreach (IListBlobItem blobItem in blobItems)
{
fileList.Add(blobItem.Uri.ToString());
}
return fileList;
}