Trying to write a file from an Azure WebJob

852 Views Asked by At

My plan is to have the WebJob send the newly generated CSV to a blob storage where it's picked up by an Azure Function and emailed out to a group of recipients.

I'm using the CsvWriter library to generate the CSV. I'm told that ideally I should upload straight to the blob storage container, however I'm not sure how I'm going to do that when CsvWriter is only able to write to the filesystem using whatever TextWriter we pass to it through the constructor.

So what I'm trying to do instead is allow CsvWriter to write the file to the filesystem, and then I'll pick that up and upload it to the blob storage. The problem here is that every directory I try to get it to write to denies access to be WebJob. I have tried Environment.GetEnvironmentVariable("WEBJOBS_ROOT_PATH") as well as %HOME% and d:\site\wwwroot.

What directory do I need to be writing to?

1

There are 1 best solutions below

2
On BEST ANSWER

You can avoid it altogether by using a StringWriter, you do not have to use the file system:

using (var stringWriter = new StringWriter())
using (var csvWriter = new CsvHelper.CsvWriter(stringWriter))
{
    csvWriter.WriteComment("Test");
    csvWriter.Flush();

    var blob = container.GetBlockBlobReference("test.csv");
    await blob.UploadTextAsync(stringWriter.ToString());
}