Harikrishna
Harikrishna

Reputation: 63

Asp.net core get all files from azure cloud blob segment

Using asp.net core return all file names from azure cloud blob storage. In blob storage their are many sub folders. In each sub folder their are around 20 k to 25 k files. On passing the sub folder name it should return all the file names in that particular folder. I am using the below code to retrieve the files

   CloudStorageAccount storageAccount =
                    CloudStorageAccount.Parse(configuration["ConnectionStrings:AzureStorageConnectionString"]);
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer container = blobClient.GetContainerReference("portal");

var blobResultSegment = await container.ListBlobsSegmentedAsync("SubFolder",
                    true, BlobListingDetails.All, int.MaxValue, null, null, null);
var totalFiles =blobResultSegment.Results;

The above code is not returning all files form that particular folder. Is their any best way to get all file name.

Upvotes: 1

Views: 1501

Answers (2)

HaBo
HaBo

Reputation: 14297

If you wan to recursively loop through all data in the container sub path, you can use something like this

public async Task FindByStation(string patternMatch)
        {
            string nextMarker = null;
            var continuationToken = new BlobContinuationToken
            {
                NextMarker = nextMarker,
            };
            var results = new List<IListBlobItem>();

            BlobResultSegment blobResultSegment;

            while (continuationToken != null)
            {
                blobResultSegment = await _blobContainer.ListBlobsSegmentedAsync(patternMatch,
                   true, BlobListingDetails.All, null, continuationToken, null, null);
                continuationToken = blobResultSegment.ContinuationToken;
                results.AddRange(blobResultSegment.Results);
            }
        }

Upvotes: 1

George Chen
George Chen

Reputation: 14324

The ListBlobsSegmentedAsync method only return a result segment containing the blob prefix. And if you want to list the blob name you need loop the result to get it.

You could refer to the below code.

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection string");
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference("test");

            var blobResultSegment = await container.ListBlobsSegmentedAsync("testfolder",true, BlobListingDetails.All, int.MaxValue, null, null, null);

            var totalFiles = blobResultSegment.Results;
            foreach(CloudBlob file in totalFiles) {
                Console.WriteLine(file.Name);
            }

And remember blob doesn't have folder, it's a virtual path, it's just the blob with a prefix foldername/blobname. If you want to the get all blob in a subfolder, the best is set the prefix with foldername/. Or it will list the blob name like the below pic.

enter image description here

Upvotes: 0

Related Questions