Search code examples
azure-blob-storage.net-core-3.1

CloudBlobDirectory' does not contain a definition for 'ListBlobs' and no accessible extension method 'ListBlobs' in .net core 3.1 upgrade


I am upgrading a .net45 app to .net core 3.1 and I have a piece of code there like below.

private void GetContainerDirectories(IEnumerable<IListBlobItem> blobList)
        {
            // First list all the actual FILES within
            // the current blob list. No recursion needed:
            foreach (var item in blobList.Where
            ((blobItem, type) => blobItem is CloudBlockBlob))
            {
                var blobFile = item as CloudBlockBlob;
                sb.Add(new Tree { Name = blobFile.Name, Id = blobFile.Name, ParentId = blobFile.Parent.Prefix, Title = Path.GetFileName(blobFile.Name), IsDirectory = false });
            }

            // List all additional subdirectories
            // in the current directory, and call recursively:
            foreach (var item in blobList.Where
            ((blobItem, type) => blobItem is CloudBlobDirectory))
            {
                var directory = item as CloudBlobDirectory;
                sb.Add(new Tree { Name = directory.Prefix, Id = directory.Prefix, ParentId = directory.Parent.Prefix, Title = new DirectoryInfo(directory.Prefix).Name, IsDirectory = true });

                // Call this method recursively to retrieve subdirectories within the current:
                GetContainerDirectories(directory.ListBlobs()); ***////////Here i am getting error***
            }
        }

In the last line [ GetContainerDirectories(directory.ListBlobs()) ], I am getting error for ListBlobs and I am not able to find any useful solution for this. The error like this -
'CloudBlobDirectory' does not contain a definition for 'ListBlobs' and no accessible extension method 'ListBlobs' accepting a first argument of type 'CloudBlobDirectory' could be found (are you missing a using directive or an assembly reference?)

Has anyone any idea how to fix this ? Many thanks in advance :)


Solution

  • The WindowsAzure.Storage SDK you are using is too old, .net core does not support the synchronous methods under this SDK, and the ListBlobs method is a synchronous method.

    I suggest you use the latest SDK instead:

    https://www.nuget.org/packages/Azure.Storage.Blobs/12.8.0

    If you don't want to use Azure.Storage.Blobs SDK, you can use ListBlobsSegmentedAsync method under WindowsAzure.Storage SDK


    Update:

    You can use the code below to instead of your original code:

        var blobs = directory.ListBlobsSegmentedAsync(false, BlobListingDetails.Metadata, 100, null, null, null).Result.Results;
        GetContainerDirectories(blobs);