Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use variables in Azure Functions out binding

I'm using Azure functions with javascript, and i would like to modify the out binding of path in my functions. For example this is my function.json:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "name": "outputBlob",
      "path": "container/{variableCreatedInFunction}-{rand-guid}",
      "connection": "storagename_STORAGE",
      "direction": "out",
      "type": "blob"
    }
  ]

I Would like to set {variableCreatedInFunction} in index.js, for example:

module.exports = async function (context, req) {
    const data = req.body
    const date = new Date().toISOString().slice(0, 10)
    const variableCreatedInFunction = `dir/path/${date}`
    if (data) {
        var responseMessage = `Good`
        var statusCode = 200
        context.bindings.outputBlob = data
    } else {
        var responseMessage = `Bad`
        var statusCode = 500
    }
    context.res = {
        status: statusCode,
        body: responseMessage
    };
}
 

Couldn't find any way to this, is it possible?

like image 723
Yaniv Irony Avatar asked Oct 15 '20 07:10

Yaniv Irony


People also ask

What is output binding in Azure functions?

Azure Function is set to trigger every 5 minutes. The function reads an image from the blob container & sends the email to the intended recipients. Timer is a Trigger. Input Binding is the one that reads the image from blob container. Sending an email to a recipient is an output binding.

Can an Azure function have multiple output bindings?

Bindings are optional and a function might have one or multiple input and/or output bindings. Triggers and bindings let you avoid hardcoding access to other services. Your function receives data (for example, the content of a queue message) in function parameters.

What are the available binding types in Azure?

For example, a function can write to a blob storage output binding, but a blob storage update could trigger another function. Some common binding types include Blob Storage, Azure Service Bus Queues, Azure Cosmos DB. Azure Event Hubs, External files, External tables and HTTP endpoints, these types are just a sample.

Can Azure function have two triggers?

An Azure function can have multiple triggers, but the function must be explicitly configured to use multiple triggers. The Azure function can have a single trigger, or it can have multiple triggers.


2 Answers

Bindings are resolved before the function executes. You can use {DateTime} as a binding expression. It will by default be yyyy-MM-ddTHH-mm-ssZ. You can use {DateTime:yyyy} as well (and other formatting patterns, as needed).

Imperative bindings (which is what you want to achieve) is only available in C# and other .NET languages, the docs says:

Binding at runtime In C# and other .NET languages, you can use an imperative binding pattern, as opposed to the declarative bindings in function.json and attributes. Imperative binding is useful when binding parameters need to be computed at runtime rather than design time. To learn more, see the C# developer reference or the C# script developer reference.

MS might've added it to JS as well by now, since I'm pretty sure I read that exact section more than a year ago, but I can't find anything related to it. Maybe you can do some digging yourself.

If your request content is JSON, the alternative is to include the path in the request, e.g.:

{
  "mypath":"a-path",
  "data":"yourdata"
}

You'd then be able to do declarative binding like this:

{
  "name": "outputBlob",
  "path": "container/{mypath}-{rand-guid}",
  "connection": "storagename_STORAGE",
  "direction": "out",
  "type": "blob"
}

In case you need the name/path to your Blob, you'd probably have to chain two functions together, where one acts as the entry point and path generator, while the other is handling the Blob (and of course the binding). It would go something like this:

  • Declare 1st function with HttpTrigger and Queue (output).
  • Have the 1st function create your "random" path containing {date}-{guid}.
  • Insert a message into the Queue output with the content {"mypath":"2020-10-15-3f3ecf20-1177-4da9-8802-c7ad9ada9a33", "data":"some-data"} (replacing the date and guid with your own generated values, of course...)
  • Declare 2nd function with QueueTrigger and your Blob-needs, still binding the Blob path as before, but without {rand-guid}, just {mypath}.
  • The mypath is now used both for the blob output (declarative) and you have the information available from the queue message.
like image 152
eli Avatar answered Nov 14 '22 23:11

eli


It is not possiable to set dynamic variable in .js and let the binding know.

The value need to be given in advance, but this way may achieve your requirement:

index.js

module.exports = async function (context, req) {
    context.bindings.outputBlob = "This is a test.";
    context.done();
    context.res = {
        body: 'Success.'
    };
}

function.json

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "name": "outputBlob",
      "path": "test/{test}",
      "connection": "str",
      "direction": "out",
      "type": "blob"
    }
  ]
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "str":"DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx;EndpointSuffix=core.windows.net"
  }
}

Or you can just put the output logic in the body of function. Just use the javascript sdk.

like image 1
Cindy Pau Avatar answered Nov 15 '22 01:11

Cindy Pau