简体   繁体   中英

Azure Blob Storage - container.GetBlobsByHierarchyAsync - not returning result

I am working with an Azure Blob Storage container that has a number of fake folder structures within and where the file(s) are housed.

I am trying to use this code block to get me the hierarchical values and the blobs so that I can return them to a calling method.

private async Task<List<BlobItem>> ListBlobsHierarchicalListing(
    BlobContainerClient container,
    string prefix)
{
    var list = new List<BlobItem>();

    var resultSegment =
        await container.GetBlobsByHierarchyAsync(prefix: prefix, delimiter: "/").ToListAsync();

    // A hierarchical listing may return both virtual directories and blobs.
    foreach (BlobHierarchyItem blobhierarchyItem in resultSegment)
    {
        if (blobhierarchyItem.IsPrefix)
        {
            // Write out the prefix of the virtual directory.
            this.logger.LogDebug("Virtual directory prefix: {0}", blobhierarchyItem.Prefix);

            // Call recursively with the prefix to traverse the virtual directory.
            list.AddRange(await this.ListBlobsHierarchicalListing(container, blobhierarchyItem.Prefix));
        }
        else
        {
            // Write out the name of the blob.
            this.logger.LogDebug("Blob name: {0}", blobhierarchyItem.Blob.Name);
            list.Add(blobhierarchyItem.Blob);
        }
    }

    return list;
}

I have seen this link Is there a way to get file structure from azure blob? where the same problem is evident and solved. However,

var resultSegment = await container.GetBlobsByHierarchyAsync(prefix: prefix, delimiter: "/").ToListAsync(); does not get me what I need. Nor does container.GetBlobsByHierarchyAsync(prefix: prefix, delimiter: "/").AsPages() as per the example. Both return with empty \ 0 values to work work.

I have my container, I have found this and this is being passed in.

the prefix is lesmis/24601/images

the container is called films and the file I am looking for is image.png

I am using nuget version 12.11.0 where this issue or an issue similar to this is said to be fixed https://github.com/Azure/azure-sdk-for.net/blob/main/sdk/storage/Azure.Storage.Blobs/CHANGELOG.md

I have additionally gone back and tried all the other previous nuget versions and still have the same result where this method is present.

Any and all help would be gratefully received.

I was able to get the data properly. Here's the code I used:

private static async Task<List<BlobItem>> ListBlobsHierarchicalListing(
    BlobContainerClient container,
    string prefix)
{
    var list = new List<BlobItem>();

    var resultSegment =
        container.GetBlobsByHierarchyAsync(prefix: prefix, delimiter: "/");

    // A hierarchical listing may return both virtual directories and blobs.
    await foreach (BlobHierarchyItem blobhierarchyItem in resultSegment)
    {
        if (blobhierarchyItem.IsPrefix)
        {
            // Write out the prefix of the virtual directory.

            // Call recursively with the prefix to traverse the virtual directory.
            list.AddRange(await ListBlobsHierarchicalListing(container, blobhierarchyItem.Prefix));
        }
        else
        {
            // Write out the name of the blob.
            list.Add(blobhierarchyItem.Blob);
        }
    }

    return list;
}

I made two changes in your code:

var resultSegment =
        await container.GetBlobsByHierarchyAsync(prefix: prefix, delimiter: "/").ToListAsync();

was changed to

var resultSegment =
    container.GetBlobsByHierarchyAsync(prefix: prefix, delimiter: "/");

and

foreach (BlobHierarchyItem blobhierarchyItem in resultSegment)

was changed to

await foreach (BlobHierarchyItem blobhierarchyItem in resultSegment)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM