Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Storage SDK - check if Blob is directory in hierarchical storage

I have an Azure Storage Account with hierarchical storage turned on. I'm getting a list of blobs in a container and trying to figure whether or not a particular blob is a directory.

I have it working in a REST API client like this:

public async Task<List<StoragePath>> ListPathsAsync(string filesystem)
{
    string uri = $"https://{accountName}.{dnsSuffix}/{filesystem}?recursive=true&resource=filesystem";
    var result = await SendRequestAsync<StoragePathList>(HttpMethod.Get, uri, CancellationToken.None, true);
    return result.Content.Paths?.ToList();
}

public class StoragePath
{
    [JsonProperty(PropertyName = "isDirectory")]
    public bool IsDirectory { get; set; }

    // other properties
}

The SendRequestAsync<T> method just uses JsonConvert to deserialize the content of the API response.

var result = JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());

Now I'm trying to figure out how to do the same thing using the .net SDK but I don't see the IsDirectory property anywhere.

public async Task<List<StoragePath>> ListPathsAsync(string filesystem)
    {
        var containerClient = new BlobContainerClient(connectionString, filesystem);
        var list = new List<StoragePath>();
        var enumerator = containerClient.GetBlobsByHierarchyAsync().GetAsyncEnumerator();

        while (await enumerator.MoveNextAsync())
        {
            var current = enumerator.Current;
            list.Add(new StoragePath()
            {
                Name = current.Blob.Name,
                //IsDirectory = current.Blob.Properties.
            });
        }

        return list;
    }

Is there some other property I should be checking?

like image 433
Connell.O'Donnell Avatar asked Dec 12 '25 20:12

Connell.O'Donnell


1 Answers

I returned to this question months later using Microsoft.WindowsAzure.Storage, Version=9.3.1.0. The CloudBlobContainer.ListBlobsSegmentedAsync(....) method returns a collection of IListBlobItem. You can determine whether each one is a directory by checking its concrete type.

I have a model class like this:

public class StoragePath
{
    public bool IsDirectory { get; set; }
    public string Name { get; set; }
}

I can populate the values using an Automapper profile as follows:

public class StorageProfile : Profile
{
    public StorageProfile()
    {
        CreateMap<IListBlobItem, StoragePath>()
            .ForMember(dest => dest.IsDirectory, prop => prop.MapFrom(src => src is CloudBlobDirectory))
            .ForMember(dest => dest.Name, prop => prop.MapFrom(src => GetName(src)));
    }

    private string GetName(IListBlobItem src)
    {
        switch (src)
        {
            case CloudBlobDirectory dir:
                return dir.Prefix;
            case CloudBlob blob:
                return blob.Name;
            default:
                throw new NotSupportedException();
        }
    }
}
like image 164
Connell.O'Donnell Avatar answered Dec 14 '25 09:12

Connell.O'Donnell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!