Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Azure Blob Service - C#

RSS
Modified on Fri, Oct 22, 2021, 10:41 AM by Administrator Categorized as Azure, ·Net Framework
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;
        }
    }

}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.