简体   繁体   English

Azure Function流C#

[英]Azure Function Stream C#

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. 我正在尝试使用Azure应用程序函数在创建Blob(json文件)时触发,然后将json展开为CSV文件并转储到其他存储位置。 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. 无论我是否写入输出流,它似乎都会创建一个0字节的文件。 Why is this file being created and how do I prevent it from occurring? 为什么要创建此文件,以及如何防止它发生?

Additional details on writing the csv contents is below: 有关编写csv内容的其他详细信息如下:

string csv_contents = "column1,column2,column3";

using (StreamWriter sw = new StreamWriter(outputBlob))
{
    sw.Write(csv_contents);
}

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. 当您输入blob为json时,输出blob为.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. 如果要使用Stream类型,我们可以将流复制到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 . 我从另一个SO线程获取演示代码json文件到csv。 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;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM