Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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,  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;
    }
like image 527
Brad Boyce Avatar asked Jun 25 '11 22:06

Brad Boyce


1 Answers

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)
like image 142
Brad Boyce Avatar answered Oct 19 '22 03:10

Brad Boyce