[英]Azure blob container - Improve performance in fetching pdf files and files details from Azure blob container
下面是我從 Azure blob 容器中獲取 pdf 文件的代碼。 大多數情況下,blob 容器最多包含 4000 個 pdf 文件。
我面臨的問題:當 blob 容器中有超過 500 個 pdf 文件時,性能非常慢並且需要更多時間來獲取。
需要輸入並幫助糾正我的代碼以提高性能。 有什么方法可以改進下面的代碼,以在 5 秒內獲取 blob 容器中可用的所有文件和文件詳細信息。
SearchQuery.cs file:
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
});
}
if (!string.IsNullOrEmpty(request.RequestModel.FullTextFilter?.Trim())) {
documentListModel = documentListModel.Where(x => x.Name.Contains(request.RequestModel.FullTextFilter))
.OrderBy(i => i.AppDate?.Date)
.ThenBy(i => i.AppDate?.TimeOfDay).ToList();
} else {
documentListModel = documentListModel
.OrderBy(i => i.AppDate?.Date)
.ThenBy(i => i.AppDate?.TimeOfDay).ToList();
}
return new WorkflowDocumentSearchRepsonse { Data = documentListModel, RecordCount = documentListModel.Count };
}
BlobManager.cs file method:
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;
}
謝謝你。
當您調用ContainerClient.GetBlobsAsync
時,會自動獲取 blob 屬性。 因此,您無需為每個 blob 調用GetPropertiesAsync
。 那應該會提高速度。
我還注意到您的代碼會檢查 blob 的元數據(這可能就是您再次獲取每個 blob 的屬性的原因)。 但是,您可以在 blob 列表操作中獲取 blob 的元數據。 您只需要在列出 blob 時包含BlobTraits
。
因此,您的列表 blob 代碼將類似於:
await foreach (BlobItem file in containerClient.GetBlobsAsync(traits: BlobTraits.Metadata, cancellationToken: cancellationToken))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.