Azure-docs: How to avoid creating an empty file when using Blob Stream function output?

Created on 29 Mar 2019  Â·  4Comments  Â·  Source: MicrosoftDocs/azure-docs

I am running an async method inside of my class function, to which I am passing the Blob output Stream object, i.e:

[FunctionName("MyTestFunction")]
public static async Task Run(
    [QueueTrigger("test-queue")]MyClass myClass,
    [Blob("test-blobs/{DateTime.Now.ToString('ddMMyyyy_HHmm')}.txt", FileAccess.Write)]Stream fileOutputStream,
    ILogger log)
{
    await myAsyncFunction(myClass, fileOutputStream);
}

However myAsyncFunction sometimes doesn't need to do write to the fileOutputStream depending on certain conditions. In this case it always creates an empty file but I would like to avoid creating a file at all.

How do I accomplish this?


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

azure-functionsvc cxp product-question triaged

Most helpful comment

@nero120 You can use Runtime Binding instead. Specifically this sample should help.

Note that still binding will create an empty file either way, so you would want to bind only if you want to write something. You might have to change your myAsyncFunction as well a bit. Final code should be something like this -

[FunctionName("MyTestFunction")]
public static async Task Run(
    [QueueTrigger("test-queue")]MyClass myClass,
    Binder binder,
    ILogger log)
{
    var data = await myAsyncFunction(myClass);

    if (data != null) {
      var attributes = new Attribute[]
      {
      new BlobAttribute($"samples-output/{DateTime.Now.ToString("ddMMyyyy_HHmm")}", FileAccess.Write),
      new StorageAccountAttribute("MyStorageAccount")
      };

      using (var fileOutputStream = await binder.BindAsync<Stream>(attributes))
      {
          // write data to fileOutputStream
      }
    }
}

MyStorageAccount is an application setting name whose value is a connection string. Depending on your setup, you could just change it to AzureWebJobsStorage too.

All 4 comments

Hi @nero120 Thank you for your feedback! We will review and update as appropriate.

@nero120 You can use Runtime Binding instead. Specifically this sample should help.

Note that still binding will create an empty file either way, so you would want to bind only if you want to write something. You might have to change your myAsyncFunction as well a bit. Final code should be something like this -

[FunctionName("MyTestFunction")]
public static async Task Run(
    [QueueTrigger("test-queue")]MyClass myClass,
    Binder binder,
    ILogger log)
{
    var data = await myAsyncFunction(myClass);

    if (data != null) {
      var attributes = new Attribute[]
      {
      new BlobAttribute($"samples-output/{DateTime.Now.ToString("ddMMyyyy_HHmm")}", FileAccess.Write),
      new StorageAccountAttribute("MyStorageAccount")
      };

      using (var fileOutputStream = await binder.BindAsync<Stream>(attributes))
      {
          // write data to fileOutputStream
      }
    }
}

MyStorageAccount is an application setting name whose value is a connection string. Depending on your setup, you could just change it to AzureWebJobsStorage too.

@nero120 Just following up here... Hope my previous comment helps.

@PramodValavala-MSFT that worked a treat, thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

behnam89 picture behnam89  Â·  3Comments

ianpowell2017 picture ianpowell2017  Â·  3Comments

monteledwards picture monteledwards  Â·  3Comments

Agazoth picture Agazoth  Â·  3Comments

DeepPuddles picture DeepPuddles  Â·  3Comments