public class AzureBlobService: IAzureBlobService { private IConfiguration _config; public AzureBlobService(IConfiguration config) { _config = config; } public byte[] GetFileContentAsBytes(BlobContainerEnum container, string fileName) { try { byte[] result; var cc = GetContainerClient(container); var bc = cc.GetBlobClient(fileName); using (var s = new MemoryStream()) { s.Seek(0, SeekOrigin.Begin); /* Don't know why, but the async version of this method tends to fail. */ /* That's why we're going with the sync method. */ //await bc.DownloadToAsync(s); bc.DownloadTo(s); result = s.ToArray(); } return result; } catch (Exception ex) { var s = ex.Message; throw; } } public long GetFileSize(BlobContainerEnum container, string fileName) { try { var cc = GetContainerClient(container); var bc = cc.GetBlobClient(fileName); var prop = bc.GetProperties().Value; return prop.ContentLength; } catch (Exception ex) { var s = ex.Recurse(); throw; } } public FileSummary GetFileSummary(BlobContainerEnum container) { try { var result = new FileSummary { Qty = 0, TotalSize = 0 }; var cc = GetContainerClient(container); var resultSegment = cc.GetBlobs().AsPages(default, 5000); foreach (var blobPage in resultSegment) { foreach (var blobItem in blobPage.Values) { var bc = cc.GetBlobClient(blobItem.Name); result.Qty += 1; Debug.Print($"Processing file {result.Qty:#,##0}"); result.TotalSize += bc.GetProperties().Value.ContentLength; } } return result; } catch (Exception ex) { var s = ex.Recurse(); throw; } } public class FileSummary { public int Qty { get; set; } public long TotalSize { get; set; } public decimal AverageSize => TotalSize / Qty; } private BlobContainerClient GetContainerClient(BlobContainerEnum name) { var scs = _config["Data:Azure:ConnectionString"]; var svcClient = new BlobServiceClient(scs); var ctnName = name.ToString().ToLowerInvariant(); var ctnClient = svcClient.GetBlobContainerClient(ctnName); ctnClient.CreateIfNotExists(); return ctnClient; } public bool PushFile( Stream fs, BlobContainerEnum container, string fileName ) { try { var cc = GetContainerClient(container); var bc = cc.GetBlobClient(fileName); var result = bc.Upload(fs); var raw = result.GetRawResponse(); return raw.Status == Microsoft.AspNetCore.Http.StatusCodes.Status201Created; } catch (Exception ex) { throw; } } }