Azure Web 函数:创建从 http 请求到 inputBlob 路径的绑定

Azure Web Function: Create binding from http request to inputBlob path

期望的场景

来自 Arduino:

问题

我不知道如何进行从路径到 HTTP 参数的绑定。

网页功能配置

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "request",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "post"
      ]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    }
  ],
  "disabled": false
}

错误:

Function ($HttpTriggerCSharp1) Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.HttpTriggerCSharp1'. Microsoft.Azure.WebJobs.Host: No binding parameter exists for 'blobName'.

原始,NON-WORKING代码(下面的工作代码):

using System.Net;

public static async Task<HttpResponseMessage> Run(
    HttpRequestMessage request, 
    string blobName,           // DOES NOT WORK but my best guess so far
    string inputBlob, 
    TraceWriter log)
{

// parse query parameter   
string msgType = request.GetQueryNameValuePairs()
    .FirstOrDefault(q => string.Compare(q.Key, "msgType", true) == 0)
    .Value;

// Get request body
dynamic data = await request.Content.ReadAsAsync<object>();

// Set name to query string or body data  
msgType = msgType ?? data?.msgType;

string deviceId = data.deviceId;
string blobName = data.BlobName; // DOES NOT COMPILE

log.Info("blobName=" + blobName); // DOES NOT WORK
log.Info("msgType=" + msgType); 
log.Info("data=" + data); 

return msgType == null
    ? request.CreateResponse(HttpStatusCode.BadRequest, "HTTP parameter must contain msgType=<type> on the query string or in the request body")
    : request.CreateResponse(HttpStatusCode.OK, "Hello " + deviceId + " inputBlob:");// + inputBlob );

}

HTTP 请求如下所示:

  https://xxxxxxprocessimagea.azurewebsites.net/api/HttpTriggerCSharp1?code=CjsO/EzhtUBMgRosqxxxxxxxxxxxxxxxxxxxxxxx0tBBqaiXNewn5A==&msgType=New image
   "deviceId": "ArduinoD1_001",
   "msgType": "New image",
   "MessageId": "12345",
   "UTC": "2017-01-08T10:45:09",
   "FullBlobName": "/xxxxxxcontainer/ArduinoD1_001/test.jpg",
   "BlobName": "test.jpg",
   "BlobSize": 9567,
   "WiFiconnects": 1,
   "ESPmemory": 7824,
   "Counter": 1

(我知道,msgType 同时出现在 URL 和 headers 中。我尝试了不同的组合 - 没有效果)。

如果我尝试做的事情是不可能的,也欢迎提出替代建议。我只是需要一个方法。

由于 Tom Sun 的提示,此代码有效。诀窍是在 JSON 中删除对存储 blob 的绑定,而是直接从代码中调用 blob。

    #r "Microsoft.WindowsAzure.Storage"
    using Microsoft.WindowsAzure.Storage; // Namespace for CloudStorageAccount
    using Microsoft.WindowsAzure.Storage.Blob; // Namespace for Blob storage types
    using Microsoft.WindowsAzure.Storage.Queue;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Host;
    using System.Net;
    using System.IO;

    public static async Task<HttpResponseMessage> Run(HttpRequestMessage request, string inputBlob, TraceWriter log)
    {
        string msgType = request.GetQueryNameValuePairs()
            .FirstOrDefault(q => string.Compare(q.Key, "msgType", true) == 0)
            .Value;

        dynamic data = await request.Content.ReadAsAsync<object>();
        msgType = msgType ?? data?.msgType;

        string deviceId = data.deviceId;
        string blobName = data.BlobName;

        string connectionString = AmbientConnectionStringProvider.Instance.GetConnectionString(ConnectionStringNames.Storage);
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference("nnriothubcontainer/" + deviceId);
        CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

        MemoryStream imageData = new MemoryStream();
        await blob.DownloadToStreamAsync(imageData);

        return msgType == null
            ? request.CreateResponse(HttpStatusCode.BadRequest, "HTTP parameter must contain msgType=<type> on the query string or in the request body")
            : request.CreateResponse(HttpStatusCode.OK, "Hello " + deviceId + " inputBlob size:" + imageData.Length);// + inputBlob );
    }

take a photo and upload image as a blob in Azure storage container (works fine)

call a Web Function using HTTP with blob name and other info (works fine) From the Web function

read the HTTP request (works fine)

根据我的理解,您可以读取Http 信息,包括blob uri、blob 名称,并尝试在Azure Http Trigger Function 中操作存储blob。 如果是这种情况,我们可以尝试参考"Microsoft.WindowsAzure.Storage" and import namespaces. Then we could operate Azure storage with Azure storeage SDK. More detail info about how to operate storage blob please refer to document

#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;