Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload video in chunks Azure blob storage

I'm receiving file content in chunks. As I've read the latest nuget recommended to work with Azure blob storage is Azure.Storage.Blobs, but I'm unable to find any example or method how to upload the file in chunks? Is it supported?

I'm receiving the range as Range header. I'm using this quickstart

like image 698
Expressingx Avatar asked Sep 03 '25 06:09

Expressingx


1 Answers

The methods you would want to use are BlockBlobClient.StageBlock which uploads the chunk data and BlockBlobClient.CommitBlock which commits the blocks and creates a block blob.

Here's a really crappy implementation of the functionality :). It basically reads a really large file from the local computer and uploads it by splitting the file in 1MB chunks.

    static void UploadBlobsInChunks()
    {
        var containerClient = new BlobContainerClient(connectionString, "test");
        containerClient.CreateIfNotExists();
        var filePath = @"C:\temp\mymovie.mp4";

        var blockBlobClient = containerClient.GetBlockBlobClient("mymovie.mp4");
        int blockSize = 1 * 1024 * 1024;//1 MB Block
        int offset = 0;
        int counter = 0;
        List<string> blockIds = new List<string>();

        using (var fs = File.OpenRead(filePath))
        {
            var bytesRemaining = fs.Length;
            do
            {
                var dataToRead = Math.Min(bytesRemaining, blockSize);
                byte[] data = new byte[dataToRead];
                var dataRead = fs.Read(data, offset, (int) dataToRead);
                bytesRemaining -= dataRead;
                if (dataRead > 0)
                {
                    var blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(counter.ToString("d6")));
                    blockBlobClient.StageBlock(blockId, new MemoryStream(data));
                    Console.WriteLine(string.Format("Block {0} uploaded successfully.", counter.ToString("d6")));
                    blockIds.Add(blockId);
                    counter++;
                }
            }
            while (bytesRemaining > 0);
            Console.WriteLine("All blocks uploaded. Now committing block list.");
            var headers = new BlobHttpHeaders()
            {
                ContentType = "video/mp4"
            };
            blockBlobClient.CommitBlockList(blockIds, headers);
            Console.WriteLine("Blob uploaded successfully!");
        }
    }
like image 164
Gaurav Mantri Avatar answered Sep 05 '25 00:09

Gaurav Mantri