gpt4 book ai didi

Azure Web功能: Create binding from http request to inputBlob path

转载 作者:行者123 更新时间:2023-12-03 03:08:52 25 4
gpt4 key购买 nike

期望的场景

来自 Arduino:

  • 拍照并将图像作为 blob 上传到 Azure 存储容器中(工作正常)
  • 使用带有 blob 名称和其他信息的 HTTP 调用 Web 函数(工作正常)通过网络功能
  • 读取 HTTP 请求(工作正常)
  • 使用 HTTP 请求中的信息读取 blob(不起作用)
  • 处理 blob(尚未实现)
  • 向Arduino响应结果

问题

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

网页功能配置

{
"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 和 header 中。我尝试了不同的组合 - 没有效果)。

如果我想做的事情是不可能的,也欢迎其他建议。我只是需要一条出路。

感谢 Tom Sun 的提示,这段代码才有效。诀窍是删除 JSON 中与存储 blob 的绑定(bind),而直接从代码中调用该 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)

根据我的理解,您可以读取包括 blob uri 、blob 名称在内的 Http 信息,并尝试在 Azure Http 触发器功能中操作存储 blob。如果是这种情况,我们可以尝试引用"Microsoft.WindowsAzure.Storage" and import namespaces 。然后我们就可以使用Azure存储SDK来操作Azure存储。更多关于如何操作存储blob的详细信息请引用document .

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

关于Azure Web功能: Create binding from http request to inputBlob path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41537011/

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