开发者

azure CloudBlobDirectory.ListBlobs() returns "The specified resource does not exist.", but fetchAttributes() works using same data

开发者 https://www.devze.com 2023-03-15 05:14 出处:网络
I am getting a \"The specified resource does not exist\" exception when I try to iterate the result of a ListBlobs() call.I can get the blob attributes when I access it directly, but I\'m trying to ge

I am getting a "The specified resource does not exist" exception when I try to iterate the result of a ListBlobs() call. I can get the blob attributes when I access it directly, but I'm trying to get a list of all the blobs in the subdirectory.

I wrote this little test to see exactly where the problem is. I have a test driver and two methods here. The first method, "GetBlockBlobDateTime" runs fine and returns a date time of an existing blob. The second method "GetBlobDirFiles" uses the same inputs and throws the excpetion when I try to iterate the blobItems at.

foreach (IListBlobItem blobItem in blobItems)

Note that the same data is used for both methods. What am I missing?

    public static void DoTest(string baseURL, string container, string directory, string fileName)
    {
        DateTime t = GetBlockBlobDateTime( baseURL,  container,  directory,  fileName);
        List<string> fileList = GetBlobDirFiles( baseURL,  con开发者_JAVA技巧tainer,  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;
    }


OK, I figured it out:

Apparently, you don't need permissions to get file attributes, but you do to list files in the directory.

    CloudBlobClient blobClient = new CloudBlobClient(baseURL);

works when you are going to fetch attributes like this:

    cloudBlockBlob.FetchAttributes();

But you need to provide credentials like this:

    CloudBlobClient blobClient = 
                    new CloudBlobClient(baseURL, 
                    new StorageCredentialsAccountAndKey(myAccount, myKey));

when you are going to list the blobs like this:

        var blobList = subDirectory.ListBlobs();
        foreach (var blobInfo in blobList)
0

精彩评论

暂无评论...
验证码 换一张
取 消