在Azure Function中处理POST请求以将文件上载到blob存储

时间:2018-05-28 22:17:35

标签: javascript azure azure-functions

我正在尝试通过简单的Azure函数HTTPTrigger在JS上传文件,它给了我以下错误:

[Error] Exception while executing function: Functions.HttpTriggerJS1. Microsoft.Azure.WebJobs.Host: Microsoft Azure WebJobs SDK '[Hidden Credential]' connection string is missing or empty. The Microsoft Azure Storage account connection string can be set in the following ways:
1. Set the connection string named '[Hidden Credential]' in the connectionStrings section of the .config file in the following format <add name="[Hidden Credential]" connectionString="DefaultEndpointsProtocol=http|https;AccountName=NAME;AccountKey=KEY" />, or
2. Set the environment variable named '[Hidden Credential]', or
3. Set corresponding property of JobHostConfiguration.

我不确定在哪里可以找到并编辑.config文件或添加环境变量。连接字符串已添加到function.json文件中,如下所示。

另外,我不知道只有context.bindings.outputBlob = req.body; 是否足以在content-type': 'application/octet-stream'的存储容器中创建blob。

index.js:

module.exports = function (context, req) {

 context.log('JavaScript HTTP trigger function processed a request.');

 context.bindings.outputBlob = req.body;

 context.res = {

 // status: 200, /* Defaults to 200 */

 body: "Uploaded " 

 };

 context.done();

};

function.json

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "route": "FileUploadNode/{filename}",
      "methods": [
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "blob",
      "name": "outputBlob",
      "path": "CONTAINER-NAME/{filename}",
      "connection": "SOME CONNECTION STRING",
      "direction": "out"
    }
  ],
  "disabled": false
}

1 个答案:

答案 0 :(得分:1)

导航至 Azure门户&gt;您的Azure功能应用&gt;平台功能&gt;应用程序设置并将存储帐户连接字符串添加为,并将function.json文件中的 SOME CONNECTION STRING 字符串添加为名称

您可以将其添加为应用程序设置或自定义连接字符串,也可以正常使用。

https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node了解更多详情。