3
votes

I am trying to write to a blob (txt-file) from my Azure function, however, nothing happens: no blobs are created, no exceptions are thrown. I've taken the Blob-trigger code from here: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob#output

This is my code:

[FunctionName("ProcessFiles")]
public static void Run(
[BlobTrigger("sample-items/sample-file.csv", Connection = "sample-connection")] Stream myBlob,
[Blob("sample-items/validationResult.json", FileAccess.Write)] Stream validationOutput,
TraceWriter log)
{
    log.Info($"C# Blob trigger function Processed blob\n Size: {myBlob.Length} Bytes");

    using (var sw = new StreamWriter(validationOutput, Encoding.UTF8))
    {
        try
        {
            sw.Write("HELLO WORLD");            
            sw.Flush();
        }
        catch (Exception ex)
        {

        }
    }
}

These are my Nuget packages: Microsoft.NET.Sdk.Functions 1.0.10.

The blob container has "Private access" policy

1

1 Answers

6
votes

It seems I was using the stream incorrect. The following code changes made it work: (added Connection attribute to validationOutput and changed from Stream to TextWriter)

[FunctionName("ProcessFiles")]
public static void Run(
[BlobTrigger("sample-items/sample-file.csv", Connection = "sample-connection")] Stream myBlob,
[Blob("sample-items/validationResult.json", FileAccess.Write, Connection = "sample-connection")] TextWriter validationOutput,
TraceWriter log)
{
    log.Info($"C# Blob trigger function Processed blob\n Size: {myBlob.Length} Bytes");

    validationOutput.WriteLine("hello world");
}