Azure blob container - Improve performance in fetching pdf files from Azure blob container.

Vamshi 151 Reputation points
2022-08-12T17:32:15.503+00:00

Hi Team,

Below is my code to fetch the pdf files from Azure blob container. Most of the times the blob container contains up to 5000 pdf files.

Issue i am facing: Performance is very slow and taking more time to fetch when there are more than 500 pdf files in the blob container.

Need inputs and help to correct my code to improve the Performance. Expecting to see if there is any way to improve the code to fetch all the files in 5 seconds..

List<WorkflowDocumentListModel> documentListModel = new List<WorkflowDocumentListModel>();
if (request.RequestModel.WorkflowStatusId == (byte)WorkflowStatus.RCV) {
IList<PdfFileMetaDataResponse> RCVFileList = await _blobManager.GetAllFilesMetaDataAsync(UserContext.AzureBlobContainerWorkflowQueue, cancellationToken).ConfigureAwait(false);
foreach (PdfFileMetaDataResponse item in RCVFileList ) {
documentListModel.Add(new WorkflowDocumentListModel {
Id = 0,
Name = item.FileName,
AssignedTo = item.CustomUserName,
AssignedToId = item.CustomUserId,
AppDate = item.LastModified?.ClientTimeToUtc(UserContext.TimeZone),
IsGrabbedByUser = userId == item.CustomUserId
});
}

 public async Task<IList<PdfFileMetaDataResponse>> GetAllFilesMetaDataAsync(string containerName, CancellationToken cancellationToken)  
        {  
            BlobServiceClient _blobServiceClient = new BlobServiceClient(UserContext.AzureBlobStorageConnection);  
            BlobContainerClient containerClient = _blobServiceClient.GetBlobContainerClient(containerName);  
            List<PdfFileMetaDataResponse> responseList = new List<PdfFileMetaDataResponse>();  
            await foreach (BlobItem file in containerClient.GetBlobsAsync(cancellationToken: cancellationToken))  
            {  
                BlobClient blobClient = containerClient.GetBlobClient(file.Name);  
                BlobProperties blobProperties = await blobClient.GetPropertiesAsync(cancellationToken: cancellationToken).ConfigureAwait(false);  
  
                DateTime? customLastModifiedDate = ConvertWebApiFileLastModifiedMillisecondsToDateTime(  
                                                blobProperties.Metadata?.Where(i => i.Key.ToUpperInvariant() == CustomBlobMetadataLastModifiedMilliseconds.ToUpperInvariant())  
                                                                                .Select(i => i.Value)  
                                                                                .FirstOrDefault());  
                string customUserName = blobProperties.Metadata?.Where(i => i.Key.ToUpperInvariant() == CustomBlobMetadataUserName.ToUpperInvariant())  
                                                                                .Select(i => i.Value)  
                                                                                .FirstOrDefault();  
  
                bool hasCustomUserID = int.TryParse(blobProperties.Metadata?.Where(i => i.Key.ToUpperInvariant() == CustomBlobMetadataUserId.ToUpperInvariant())  
                                                                .Select(i => i.Value).FirstOrDefault(), out int customUserID);  
  
                PdfFileMetaDataResponse response = new PdfFileMetaDataResponse()  
                {  
                    FileName = file.Name,  
                    CustomUserName = customUserName,  
                    CustomUserId = hasCustomUserID ? customUserID : (int?)null,  
                    LastModified = customLastModifiedDate ?? file.Properties.LastModified?.UtcDateTime  
                };  
  
                responseList.Add(response);  
            }  
  
            return responseList;  
        }  

Thank you.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,426 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,154 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,228 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. SaiKishor-MSFT 17,181 Reputation points
    2022-08-15T18:55:15.58+00:00

    @Vamshi I see your question was answered on SO- https://stackoverflow.com/questions/73349120/azure-blob-container-improve-performance-in-fetching-pdf-files-and-files-detai

    If you need any further assistance, please do let us know. Thank you!

    1 person found this answer helpful.
    0 comments No comments