Home » c# » c# – Azure Blob Storage Update-Exceptionshub

c# – Azure Blob Storage Update-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I am trying to read the content from Azure BlockBlob, apply some transformation and write the content back to the same blob in Azure Function.

The code is below for writing the content back. It runs success. However, when I examine the content in the final blob, it only has partial of the data. e.g. there should be 28 rows of data in the content, but the content only has 26 and half rows, it seems the part of content get lost after calling “UploadFromStreamAsync”. I see lost of examples on the web but they are all about uploading brand new files. Am I missing something in the code when updating blob?

    protected async Task WriteItemsToBlog(string blobName, List<T> items)
    {

        var cloudStorageAccount =
            new CloudStorageAccount("MyStorageCredentials", "MyAccountName", null, true);

        var cloudBlobClient =
            cloudStorageAccount.CreateCloudBlobClient();

        var cloudBlobContainer =
            cloudBlobClient.GetContainerReference("MyContainerPath");

        var cloudBlockBlob =
            cloudBlobContainer.GetBlockBlobReference(blobName);

        var ms = new MemoryStream();

        using (var writer = new StreamWriter(ms))
        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
        {
            csv.WriteRecords(items);                 
            ms.Position = 0;
            cloudBlockBlob.Properties.ContentType = "text/plain";

            await cloudBlockBlob.UploadFromStreamAsync(ms, ms.Length);
        }
    }
How to&Answers: