【问题标题】:Getting parameters from an Azure Function to use in function.json bindings从 Azure 函数获取参数以在 function.json 绑定中使用
【发布时间】:2021-01-12 15:07:14
【问题描述】:

我是 Azure Functions 的新手,在一些基础知识方面遇到了问题,特别是如何将参数数据传递给 function.json,以便我可以使用存储连接器将 blob 写入 Azure Blob 存储。

我的问题是,如何在 httpTrigger 函数中指定一个可供下面的 outputBlobContents 绑定使用的参数?

我的设置很简单(但还不行):


function.json:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "blob",
      "direction": "out",
      "name": "outputBlobContents",
      "path": "uploaded_files/{destinationFilename}",
      "connection": "MY_STORAGE"
    }
  ]
}

index.ts:

import { AzureFunction, Context, HttpRequest } from "@azure/functions"

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
    context.bindingData.destinationFilename = "testfile.pdf";
    context.bindings.outputBlobContents = context.req.body;
    const responseMessage = "uploaded file";

    context.res = {
        status: 201,
        body: responseMessage
    };

};

export default httpTrigger;

在我的示例代码中,我尝试将destinationFilename 分配给context.bindingData 对象,但这不起作用。我已经阅读了所有文档,但对于 bindingData 对象实际上是什么或命名参数通常如何工作并不是很清楚。如何告诉 blob 存储连接器将文件存储在哪里?

【问题讨论】:

    标签: javascript azure azure-functions


    【解决方案1】:

    简单来说,任何形式的json输入都会作为参数获取。里面的函数是不可能的。如果要在函数内部使用,请选择直接使用Azure Storage的sdk。

    由于你使用httptrigger,你可以发送一个json格式的请求体,像这样:

    {
       "destinationFilename":"something"
    }
    

    之后,输出绑定就可以拿到值了。

    【讨论】:

    • 谢谢,我可以使用 Azure 存储 SDK。我希望文档能更清楚地说明绑定可以做什么和不能做什么。
    猜你喜欢
    • 2021-04-04
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多