Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions (Python) blob ouput binding. How to set name when name is only part of the input message

I have an Azure Functions (Python 3) function that takes a message from a Service Bus queue and creates a Blob in a container as a result.

The function trigger is the Sevice Bus message. This message is a JSON object with several properties, one of which is the blob name.

The docs suggest something like this in the bindings:

{
      "name": "outputblob",
      "type": "blob",
      "path": "samples-workitems/{queueTrigger}-Copy",
      "connection": "MyStorageConnectionAppSetting",
      "direction": "out"
    }

But this suggest that the triggering message contains just the blob name. I can not make the message solely the blob name as I require the other attributes in the message to determine what to do / what data to put in the blob.

Is there any way to use the output bindings that will resolve this for me?

Thanks.

like image 212
tam203 Avatar asked Dec 18 '19 15:12

tam203


People also ask

Can an Azure function have multiple output bindings?

Azure Function Bindings An output binding is the data that your function sends. Unlike a trigger, a function can have multiple input and output bindings.

What is input and output binding in Azure functions?

Binding is the connection to data within your Azure Functions. There are two types of Bindings. Input Binding: A input binding is the data that your function receives. Output Binding: A output binding is the data that your function sends.

How do you use the blob trigger in Azure function?

The function writes a log when a blob is added or updated in the samples-workitems container. The string {name} in the blob trigger path samples-workitems/{name} creates a binding expression that you can use in function code to access the file name of the triggering blob.


1 Answers

Yes, this could be done. You could just set the input and output binding path with the json value from the trigger json data. The below is my function.json. Use service bus trigger get the input blob name and output blob name, then write the input blob to the output blob. You could also set the container name with this way.

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "msg",
      "type": "serviceBusTrigger",
      "direction": "in",
      "queueName": "myqueue",
      "connection": "servicebuscon"
    },
    {
      "name": "inputblob",
      "type": "blob",
      "path": "inputcontainer/{blobname}",
      "connection": "AzureWebJobsStorage",
      "direction": "in"
    },
    {
      "name": "outputblob",
      "type": "blob",
      "path": "outputcontainer/{outblobname}",
      "connection": "AzureWebJobsStorage",
      "direction": "out"
    }
  ]
}

And the below is the function code.

import logging

import azure.functions as func
import json, os


def main(msg: func.ServiceBusMessage,inputblob: func.InputStream,outputblob: func.Out[bytes]) -> func.InputStream:
    logging.info('Python ServiceBus queue trigger processed message: %s',
                 msg.get_body().decode('utf-8'))
    jsonData= json.loads(inputblob.read())
    logging.info(jsonData)
    outputblob.set(str(jsonData))

And I set the service bus message like below message.

enter image description here

Here is the result pic. You could find the input blob json data shown in the console and I check the container the output blob is created.

enter image description here

like image 119
George Chen Avatar answered Oct 17 '22 14:10

George Chen