当我尝试迭代ListBlobs()调用的结果时,我得到“指定的资源不存在”异常。我直接访问它时可以获取blob属性,但我正在尝试获取子目录中所有blob的列表。
我写了这个小测试,看看问题的确切位置。我有一个测试驱动程序和两个方法。第一个方法“GetBlockBlobDateTime”运行正常并返回现有blob的日期时间。当我尝试迭代blobItems时,第二种方法“GetBlobDirFiles”使用相同的输入并抛出excpetion。
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;
}
答案 0 :(得分:13)
好的,我明白了:
显然,您不需要获取文件属性的权限,但您可以列出目录中的文件。
CloudBlobClient blobClient = new CloudBlobClient(baseURL);
当您要获取这样的属性时起作用:
cloudBlockBlob.FetchAttributes();
但是你需要提供这样的凭证:
CloudBlobClient blobClient =
new CloudBlobClient(baseURL,
new StorageCredentialsAccountAndKey(myAccount, myKey));
当你打算列出这样的blob时:
var blobList = subDirectory.ListBlobs();
foreach (var blobInfo in blobList)