List Azure Storage Blobs in Entity Framework Core

I am trying to get a list of blobs from out azure storage. I have the list working on Entity Framework, but I am using EF Core and I just can't seem to get it to work. I know my connections still work and everything, I put a break point down to make sure of that. The problem is that when I await the ListBlobsSegmentedync method it just freezes, I don't an error it just stops.

Here is the code in question:

public async Task<IList<CloudBlockBlob>> ListJobBlobsAsync(Guid jobId)
{
    try
    {
        //Connect to Azure using the above credentials
        CloudBlobClient client = new CloudBlobClient(new Uri("https://pdmacstorage.blob.core.windows.net/"), credentials);

        //Get refrence to the container
        CloudBlobContainer container = client.GetContainerReference(folderJobPath);

        //Create if it doesn't exist
        await container.CreateIfNotExistsAsync();

        BlobContinuationToken continuationToken = null;
        BlobResultSegment blobsList = null;


        //Create list to hold blobs
        IList<CloudBlockBlob> blobs = new List<CloudBlockBlob>();

        blobsList = await container.ListBlobsSegmentedAsync(jobId.ToString(), true, BlobListingDetails.All, int.MaxValue, continuationToken, null, null);

        if(blobsList != null)
        {
            //Loop through the blobs in the container and filter the blobs
            foreach(IListBlobItem item in blobsList.Results)
            {
                if(item.GetType() == typeof(CloudBlockBlob))
                {
                    blobs.Add((CloudBlockBlob) item);
                }
            }

            continuationToken = blobsList.ContinuationToken;
        }

        return blobs;
    }
    catch(Exception ex)
    {
        return null;
    }
}

It stops on this line:

blobsList = await container.ListBlobsSegmentedAsync(jobId.ToString(), true, BlobListingDetails.All, int.MaxValue, continuationToken, null, null);

I clearly don't have the best understanding of synchronous or asynchronous methods, but if someone could possibly point me in the right direct that would be awesome. Any help is greatly appreciated.

What would cause this code to fail?

1 answer

  • answered 2018-07-21 09:22 Lee Liu

    I have tested your code and it worked fine with my Azure Storage.

    You said that it stopped on the code:

    blobsList = await container.ListBlobsSegmentedAsync(jobId.ToString(), true, BlobListingDetails.All, int.MaxValue, continuationToken, null, null);
    

    I think the reason for this problem is that you set the parameter int? maxResults to be Max of int type. This caused the high delay because it has too large counts of results. I recommend you to set this parameter to be a Smaller value, for example, we can set to be 100,200... Please try a Smaller value, if the problem still occurs, you can tell us for further research.

    Here is my tested code for your reference:

        public static async Task<List<CloudBlockBlob>> ListJobBlobsAsync(string jobId, string containerName, int maxValue)
        {
            try
            {
                //Connect to Azure using the above credentials
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=xxxxx;AccountKey=O7xB6ebGxxxxxxx/x/e9l9FhLqayXcbz1R+E0mxxxxxxPYY5dF2JrAVHtBozbJo29ZrrGJA==;BlobEndpoint=https://leeliublob.blob.core.windows.net/;QueueEndpoint=https://leeliublob.queue.core.windows.net/;TableEndpoint=https://leeliublob.table.core.windows.net/;FileEndpoint=https://leeliublob.file.core.windows.net/;");
    
                CloudBlobClient client = storageAccount.CreateCloudBlobClient();
    
                //Get refrence to the container
                CloudBlobContainer container = client.GetContainerReference(containerName);
    
                //Create if it doesn't exist
                await container.CreateIfNotExistsAsync();
    
                BlobContinuationToken continuationToken = null;
                BlobResultSegment blobsList = null;
    
    
                //Create list to hold blobs
                List<CloudBlockBlob> blobs = new List<CloudBlockBlob>();
    
                blobsList = await container.ListBlobsSegmentedAsync(jobId, true, BlobListingDetails.All, maxValue, continuationToken, null, null);
    
                if (blobsList != null)
                {
                    //Loop through the blobs in the container and filter the blobs
                    foreach (IListBlobItem item in blobsList.Results)
                    {
                        if (item.GetType() == typeof(CloudBlockBlob))
                        {
                            blobs.Add((CloudBlockBlob)item);
                        }
                    }
    
                    continuationToken = blobsList.ContinuationToken;
                }
    
                return blobs;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    

    Screenshot of result:

    enter image description here