I'm trying to create a CSV file and to import it to an Azure storage account.
public static void ExportCsvToStorageAccount(string fileName, CloudBlobContainer container, IEnumerable<ReportRow> reportEntries)
{
using (var ms = new MemoryStream())
{
using (var file = new StreamWriter(ms))
{
file.WriteLine("Date,StoreId,ItemId,SalesQuantity");
foreach (var row in reportEntries)
{
var line = $"\"{row.Date}\",\"{row.StoreId}\",\"{row.ItemId}\",\"{row.SalesQuantity}\"";
file.WriteLine(line);
}
var blockBlob = container.GetBlockBlobReference($"{fileName}.csv");
ms.Position = 0;
blockBlob.UploadFromStream(ms);
}
}
}
I'm creating the file in memory and then copying it and uploading it to azure.
My "issue" is that for this i need to first save the whole file in memory and only then start copying (it can be an issue if the file is too big and the machine is low on ram).
Ideally i could write directly into azure or as soon as i filled my memory stream buffer i would copy it to azure and then write again on top of it instead of allocating more space in me memory stream buffer.
Is there a way to write directly into Azure? (The objective is to save ram)
Edit:
With the input of the answer by Gaurav Mantri-AIS I came up with this (because i have more than 50000 entries which is the limit of blocks),
public static void ExportCSVToStorageAccount(string fileName, CloudBlobContainer container, IEnumerable<RawReportRow> reportEntries)
{
var blob = container.GetAppendBlobReference($"{fileName}.csv");
blob.CreateOrReplace();
blob.AppendText($"Date,StoreId,ItemId,SalesQuantity{Environment.NewLine}");
foreach (var row in reportEntries)
{
var line = $"\"{row.Date}\",\"{row.StoreId}\",\"{row.ItemId}\",\"{row.SalesQuantity}\"{Environment.NewLine}";
blob.AppendText(line);
}
}
The issue with this solution is that it takes too long, from 5 minutes to more than an hour. I'm probably doing something wrong as the AppendBlob should perform well appending, but it doesn't seem to be the case.
Any Idea in how to improve the write speed a little bit?