I'm developing a file uploader whichs use Kendo UI to upload the file chunk by chunk. The back-end pf my website is ASP.NET core MVC and the Microsoft Azure is used to store the uploaded files. The Kendo UI chunk uploader sends the chunks to the back-end and I write those chunks to an Azure blob using PutBlockAsync
method. Once all the chunks are received, I call PutBlockListAsync
to commit the received chunks to azure so that it should create the uploaded file in Azure blob. The debugger does not show any errors when calling these two methods but in Azure blob, the uploaded file is not creating.
Below are the methods I've written to call above two methods,
public async Task UploadInBlocksToAzure(string id, Stream stream)
{
try
{
var storageAccount = CloudStorageAccount.Parse(_connectionString);
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
var container = cloudBlobClient.GetContainerReference("test-video-in");
var blob = container.GetBlockBlobReference("a.mp4");
await blob.PutBlockAsync(id, stream, null);
}
catch (Exception e)
{
throw;
}
}
And this method is to commit the file
public async Task CommitBlocksToAzure(IEnumerable<string> ids)
{
try
{
var storageAccount = CloudStorageAccount.Parse(_connectionString);
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
var container = cloudBlobClient.GetContainerReference("test-video-in");
var blob = container.GetBlockBlobReference("a.mp4");
await blob.PutBlockListAsync(ids);
}
catch (Exception)
{
throw;
}
}
The id parameter in UploadInBlocksToAzure(string id, Stream stream)
method provides the Base64-encoded string that identifies the block. So in my application, let say I'm uploading 5.5MB file and the chunk size is 1MB, then there will be 5 chunks and id
of each chunk are 0
,1
,2
,3
,4
and 5
. When I convert these numbers into Base64 strings using Convert.ToBase64String(BitConverter.GetBytes(chunkIndex))
, I get chunk ids as AAAAAA==
, AQAAAA==
, AgAAAA==
, AwAAAA==
, BAAAAA==
and BQAAAA==
.
Is there something wrong in the approach I'm following?
Update: When I change the logic to create the Base64 encoded string for the block id, the file was successfully committed to the azure blob but the file is currupted.
From
var id = Convert.ToBase64String(BitConverter.GetBytes(chunkData.ChunkIndex));
To
var id = Convert.ToBase64String(Encoding.UTF8.GetBytes(chunkData.ChunkIndex.ToString("d6")));
There was nothing wrong with the way I've been doing this task, but I've verified the uploaded video by downloading it and playing it in the windows media player. The WMP shows an error saying that the file may be corrupted (the original file is playing in the WMP).
But when I copy the blob URL of the uploaded file from Azure and browse it in the web browser the video was playing without any issues.
The only issue in the code was the way I create the block ids. The below approach worked for me,
var id = Convert.ToBase64String(Encoding.UTF8.GetBytes(chunkData.ChunkIndex.ToString("d6")));