0

I am trying to set up an azure function that would write into a blob only if a function is fulfilled. The blob is int he same location as the function, so I am trying to avoid providing a connection string and do this with bindings. I am currently using binding something like the following:

[Blob("folder/myFile.json", FileAccess.Write)]Stream writeBlob

With this binding, I can write into the JSON file using:

if (myCondition)
    using (var writer = new StreamWriter(writeBlob)) 
        writer.Write(myContent);

This works fine when the condition is true. However, when the condition is false, the file gets empty. Since I am not writing to the stream, I expected the file to stay untouched. Right now my workaround is to have another read binding to the same json and rewrite the file contents.

Neville Nazerane
  • 6,622
  • 3
  • 46
  • 79
  • 2
    You could bind to container CloudBlobContainer instead of blob and create a blob on need basis – Siri Feb 17 '19 at 06:42

1 Answers1

2

You can use dynamic binding in your case. See this post for more information:

So basically you need to:

  • Add a IBinder binder parameter in your function definition.
  • When your condition is true, write your file:
    if (myCondition)
    {
        var binding= new BlobAttribute(blobPath: "folder/myFile.json");
        using (var writer = binder.Bind<TextWriter>(binding))
        {
            writer.Write(myContent);
        }
    }
    
Thomas
  • 24,234
  • 6
  • 81
  • 125