gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:10:17 25 4
gpt4 key购买 nike

我花了很多时间,但没有找到任何关于如何从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
}
}
};

最佳答案

因为nodejs函数不支持绑定(bind)容器,所以如果你想绑定(bind)动态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"
};
}
};

然后在 postman 中,我发送以下 json 数据,发送请求,该函数会将 blob 与 001.txt 绑定(bind)。

{
"outblobname":"001.txt"
}

关于javascript - Azure 函数 blob 输出绑定(bind)路径参数 (javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59581748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com