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?
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
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
}
}
}
MyStorageAccountis an application setting name whose value is a connection string. Depending on your setup, you could just change it toAzureWebJobsStoragetoo.
@nero120 Just following up here... Hope my previous comment helps.
@PramodValavala-MSFT that worked a treat, thank you!
Most helpful comment
@nero120 You can use
Runtime Bindinginstead. 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
myAsyncFunctionas well a bit. Final code should be something like this -