所需场景
来自 Arduino:
- 在 Azure 存储容器中拍照并将图像作为 blob 上传(工作正常)
- 使用带有 blob 名称和其他信息的 HTTP 调用 Web 函数(工作正常) 从 Web 函数
- 阅读 HTTP 请求(工作正常)
- 使用来自 HTTP 请求的信息读取 blob(不起作用)
- 处理 blob(尚未实现)
- 向 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'.
原始,非工作代码(下面的工作代码):
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 和标题中。我尝试了不同的组合 - 没有效果)。
如果我想做的事情是不可能的,也欢迎其他建议。我只需要一条路。
由于 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 );
}