简体   繁体   中英

Azure Function TextWriter stream is appending to file and not overwriting - do I need to wrap in a using statement?

I have an Azure Storage account where I upload a file. The file upload takes place in an Azure Function (Blob trigger).

Everything works as expected, however, if I trigger the Function, three times, the file is appended with previous three sessions data. I don't think I am disposing of the stream correctly.

 public void Run(
        [BlobTrigger("raw/{name}")] Stream input,
        [Blob("data/{name}", FileAccess.Write)] TextWriter output,
        string name,
        ILogger log)

I am then writing to the output as follows:

output.WriteLine(data);

I am not sure how to wrap this in a using statement.

Can anyone advise?

Here is the code that worked for me where each time I'm triggering the function the file is getting overwritten with the current triggered data.

using System;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace FunctionAppTW
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([BlobTrigger("raw/{name}", Connection = "")] Stream input, [Blob("data/{name}", FileAccess.Write)] TextWriter output, string name, ILogger log)
        {

            StreamReader reader = new StreamReader(input);
            string data = reader.ReadToEnd();

            output.WriteLine(data);
        }
    }
}

RESULT:

在此处输入图像描述

在此处输入图像描述

I've run into this issue before myself. I can't recall but I used dependency injection in my functions app. My implementation code kept the stream alive and would aggregate previous runs when using just the output.writeline() statement.

I had to close and dispose of the previous session before each and every method call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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