8

当我尝试迭代 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;
    }
4

1 回答 1

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)
于 2011-06-26T15:30:58.437 回答