In Azure Durable functions, I want to bind to Blob storage and write to a text file.
The following code will write it as a new file instead of writing it as an additional file.
What is the code to write as an addition to text.txt in newblob?
import azure.functions as func
import azure.durable_functions as df
import logging
import json
app = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)
### client function ###
@app.route(route="orchestrators/client_function")
@app.durable_client_input(client_name="client")
async def client_function(req: func.HttpRequest, client: df.DurableOrchestrationClient) -> func.HttpResponse:
instance_id = await client.start_new("orchestrator", None, {})
logging.info(f"Started orchestration with ID = '{instance_id}'.")
await client.wait_for_completion_or_create_check_status_response(req, instance_id)
status = await client.get_status(instance_id)
return f"output: {status.output}"
### orchestrator function ###
@app.orchestration_trigger(context_name="context")
def orchestrator(context: df.DurableOrchestrationContext) -> str:
result = yield context.call_activity("main", '')
return "Inserted"
### activity function ###
@app.blob_output(arg_name="outputblob", path="newblob/test.txt", connection="BlobStorageConnection")
@app.activity_trigger(input_name="blank")
def main(blank: str, outputblob: func.Out[str]):
data = {"size": 100, "time": 0.1}
json_data = json.dumps(data)
outputblob.set(json_data)
return "Inserted"
In Azure Functions, when you
bind
to Blob Storage to write data, the behavior is to create or overwrite the specified blob. If you want to append to an existing blob, you have to handle that logic yourself and not use bindingTo append data to a blob in Azure Functions:
Modified
main
function to achieve this as an example:Replace
"YOUR_CONNECTION_STRING"
with your Blob Storage connection string.