0

I'm trying to use an Azure app function to trigger when a blob (json file) is created, and then unwind the json into a CSV file and dump into a different storage location. However, when I use an output binding like:

[Blob("test-ex/{name}.csv", FileAccess.Write, Connection = "my_storage_account")] Stream outputBlob

it seems to create a 0 byte file regardless of whether I write to the output stream or not. Why is this file being created and how do I prevent it from occurring?

Additional details on writing the csv contents is below:

string csv_contents = "column1,column2,column3";

using (StreamWriter sw = new StreamWriter(outputBlob))
{
    sw.Write(csv_contents);
}
Joy Wang
  • 39,905
  • 3
  • 30
  • 54
codedawg82
  • 466
  • 1
  • 12
  • 25

1 Answers1

-1

Why is this file being created and how do I prevent it from occurring?

As you input blob is json and output blob is .csv. I recommend that you could use string type directly.

demo code:

public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "AzureWebJobsStorage")]string myBlob, string name, [Blob("test2/{name}.csv", FileAccess.Write, Connection = "AzureWebJobsStorage")]out string outputBlob,  TraceWriter log)
{
            var outputstring = jsonToCSV(myBlob, ","); // add your logic to covert json to CSV

            log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
            outputBlob = outputstring;
 }

If you want to use the Stream type we could copy the stream to the outputBlob.

 public static void Run([BlobTrigger("test/{name}", Connection = "AzureWebJobsStorage")]Stream myBlob, string name, [Blob("test2/{name}.csv", FileAccess.Write, Connection = "AzureWebJobsStorage")] Stream outputBlob, TraceWriter log)
  {
            myBlob.Position = 0;
            var str = StreamToString(myBlob);
            var outputstring =jsonToCSV(str,",");// add your logic to covert json to CSV
            var stream = StringtoStream(outputstring);
            stream.Position = 0;
            stream.CopyTo(outputBlob);
            log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");

   }

I get the demo code json file to csv from another SO thread. You could deal it with yourself.

 public static DataTable jsonStringToTable(string jsonContent)
 {
       DataTable dt = JsonConvert.DeserializeObject<DataTable>(jsonContent);
       return dt;
 }
 public static string jsonToCSV(string jsonContent, string delimiter)
 {
        StringWriter csvString = new StringWriter();
        using (var csv = new CsvWriter(csvString))
        {

            csv.Configuration.Delimiter = delimiter;

            using (var dt = jsonStringToTable(jsonContent))
            {

                foreach (DataColumn column in dt.Columns)
                {
                    csv.WriteField(column.ColumnName);
                }
                csv.NextRecord();

                foreach (DataRow row in dt.Rows)
                {
                    for (var i = 0; i < dt.Columns.Count; i++)
                    {
                        csv.WriteField(row[i]);
                    }
                    csv.NextRecord();
                }
            }
        }
        return csvString.ToString();
    }

  public static string StreamToString(Stream stream)
  {
        StreamReader reader = new StreamReader(stream);
        string text = reader.ReadToEnd();
        return text;
  }
  public static Stream StringtoStream(string str)
  {
        byte[] byteArray = Encoding.UTF8.GetBytes(str);
        MemoryStream stream = new MemoryStream(byteArray);
        return stream;
  }
Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47