5
votes

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.

1

1 Answers

7
votes

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