【问题标题】:Azure function blob output binding path parameter (javascript)Azure 函数 blob 输出绑定路径参数 (javascript)
【发布时间】:2020-04-22 04:55:23
【问题描述】:

我花了很多时间,但我没有找到任何关于如何从 Azure 函数设置 blob 路径的路径参数的 javascript 示例或文档。如何从函数内部设置"path": "randomnum-container/{filename}" 的{filename}?

function.json

{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "blob",
      "name": "outputBlob",
      "path": "randomnum-container/{filename}",
      "connection": "randomnum_STORAGE",
      "direction": "out"
    }
  ],
  "disabled": false
}

index.js

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    const max = 100;
    const num = Math.floor(Math.random() * Math.floor(max));
    // how to set outputBlob {filename} parameter?
    // let's say I want to save every request to {num}.txt
    context.res = {
        status: 200,
        body: {
            "number": num
        }
    }
};

【问题讨论】:

    标签: javascript azure function blob


    【解决方案1】:

    由于nodejs函数不支持绑定容器,所以如果要绑定动态blob,必须在函数运行前定义。

    并且您正在使用 HTTP 触发器功能,因此您可以在请求中定义 blob 名称。以下是我的功能。

    我的function.json:

    {
      "bindings": [
        {
          "authLevel": "function",
          "type": "httpTrigger",
          "direction": "in",
          "name": "req",
          "methods": [
            "get",
            "post"
          ]
        },
        {
          "type": "http",
          "direction": "out",
          "name": "res"
        },{
          "name": "outputblob",
          "type": "blob",
          "path": "outputcontainer/{outblobname}",
          "connection": "AzureWebJobsStorage",
          "direction": "out"
        }
      ]
    }
    

    index.js:

    module.exports = async function (context, req) {
        context.log('JavaScript HTTP trigger function processed a request.');
    
        context.bindings.outputblob = "test string";
        context.done();
    
    
        if (req.query.name || (req.body && req.body.name)) {
            context.res = {
                // status: 200, /* Defaults to 200 */
                body: "Hello " + (req.query.name || req.body.name)
            };
        }
        else {
            context.res = {
                status: 400,
                body: "Please pass a name on the query string or in the request body"
            };
        }
    };
    

    然后在邮递员中,我发送下面的json数据,发送请求,函数会将blob与001.txt绑定。

    {
    "outblobname":"001.txt"
    }
    

    【讨论】:

      【解决方案2】:

      谢谢乔治!

      我找到了这个解决方案和另一个解决方案。此解决方案的缺点是,我无法在无服务器功能中操作文件名。我做的另一个灵魂是持久的功能。

      sendMessage.js(启动器)

      const df = require("durable-functions");
      
      module.exports = async function (context, req) {
          const client = df.getClient(context);
          const instanceId = await client.startNew(req.params.functionName, undefined, req.body);
      
          context.log(`Started orchestration with ID = '${instanceId}'.`);
      
          return client.createCheckStatusResponse(context.bindingData.req, instanceId);
      };
      

      function.json (sendMessage.js)

      {
        "bindings": [
          {
            "authLevel": "function",
            "name": "req",
            "type": "httpTrigger",
            "direction": "in",
            "route": "orchestrators/{functionName}",
            "methods": [
              "post",
              "get"
            ]
          },
          {
            "name": "$return",
            "type": "http",
            "direction": "out"
          },
          {
            "name": "starter",
            "type": "orchestrationClient",
            "direction": "in"
          }
        ]
      }
      

      messageOrchestrator.js

      const df = require("durable-functions");
      
      module.exports = df.orchestrator(function* (context) {
          const outputs = [];
          const input = context.df.getInput();
          input['filename'] = `file_${input['name']}.txt`;
      
          outputs.push(yield context.df.callActivity("logMessage", input));
      
      
          return outputs;
      });
      

      function.json (messageOrchestrator.js)

      {
        "bindings": [
          {
            "name": "context",
            "type": "orchestrationTrigger",
            "direction": "in"
          }
        ]
      }
      

      logMessage.js(活动)

      module.exports = async function (context) {
        context.bindings.filestore = context.bindings.payload;
        return `Hello ${context.bindings.payload}!`;
      };
      

      function.json (logMessage.js)

      {
        "bindings": [
          {
            "name": "payload",
            "type": "activityTrigger",
            "direction": "in"
          },
          {
            "name": "filestore",
            "type": "blob",
            "path": "{messagetype}/{filename}",
            "direction": "out",
            "connection": "AzureWebJobsStorage"
          }
        ]
      }
      

      重要的东西在 messageOrchestrator 和 logMessage.js 的 function.json 中。我在编排器中操作文件名,并将有效负载的属性绑定到活动的 function.json 中。

      这对我很有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-02
        相关资源
        最近更新 更多