Azure函数blob输出绑定路径参数(javascript)

huangapple go评论74阅读模式
英文:

Azure function blob output binding path parameter (javascript)

问题

I spent a lot of time, but I did not find any javascript example or document about how can I set a path parameter of a blob path from Azure function. How can I set the {filename} of "path": "randomnum-container/{filename}", from the function inside?

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
}
}
};

英文:

I spent a lot of time, but I did not find any javascript example or document about how can I set a path parameter of a blob path from Azure function. How can I set the {filename} of "path": "randomnum-container/{filename}", from the function inside?

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
        }
    }
};

答案1

得分: 2

The text you provided appears to be code-related and includes instructions and configuration for a Node.js function using Azure Functions and an HTTP trigger. Here's a translation of the code-related content:

由于 Node.js 函数不支持绑定容器,所以如果您想要绑定动态 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 = {
            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"
        };
    }
};

然后在 Postman 中,我发送以下 JSON 数据,发送请求,函数将绑定名为 001.txt 的 blob。

{
    "outblobname": "001.txt"
}

请注意,此内容是与代码相关的翻译,仅包含代码和配置的部分。

英文:

Cause the nodejs function doesn't support to bind the container, so if you want to bind the dynamic blob, you have to define it before the function running.

And you are using the HTTP trigger function, so you could define the blob name in the request. The below is my function.

My 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"
        };
    }
};

Then in the postman, I send the below json data, send the request, the function will bind the blob with 001.txt.

{
"outblobname":"001.txt"
}

答案2

得分: 1

感谢George!

我找到了这个解决方案和另一个解决方案。这个解决方案的缺点是我无法在无服务器函数中操纵文件名。我制作的另一个解决方案是可持久化函数。

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中。我在orchestrator中操纵了文件名,并在activity的function.json中绑定了payload的属性。

这对我很有效。

英文:

Thanks George!

I found this solution and another one. The drawback of this solution, I can not manipulate the file name in the serverless function. The other soultion I've made is a durable function.

sendMessage.js (starter)

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 (activity)

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"
    }
  ]
}

The important things are in the messageOrchestrator and in the logMessage.js's function.json. I manipulate filename in the orchestrator and binding the payload's attribute in the activity's function.json.

This worked well for me.

huangapple
  • 本文由 发表于 2020年1月4日 00:09:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定