1

望ましいシナリオ

Arduino から:

  • 写真を撮り、イメージを BLOB として Azure ストレージ コンテナーにアップロードします (正常に動作します)。
  • HTTP を使用して BLOB 名とその他の情報を使用して Web 関数を呼び出します (正常に動作します) Web 関数から
  • HTTP リクエストを読み取ります (正常に動作します)
  • HTTP 要求からの情報を使用して 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 );
    }
4

1 に答える 1

1

写真を撮り、イメージを Azure ストレージ コンテナーに BLOB としてアップロードします (正常に動作します)。

HTTP を使用して BLOB 名とその他の情報を使用して Web 関数を呼び出します (正常に動作します) Web 関数から

HTTP リクエストを読み取ります (正常に動作します)

私の理解に基づいて、blob uri、blob name を含む Http 情報を読み取り、Azure Http Trigger Function でストレージ blob を操作してみることができます。その場合は、 "Microsoft.WindowsAzure.Storage" を参照して名前空間をインポートしてみてください。その後、Azure ストレージ SDK を使用して Azure ストレージを操作できます。ストレージ BLOB の操作方法の詳細については、ドキュメントを参照してください。

#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
于 2017-01-10T03:17:59.053 に答える