2017-04-11 9 views
0

Azure(Blob)ストレージのblobに格納されているテキストを読み込めません。Azure:Azure関数からBlobテキストを読み取ることができません。HttpTrigger(400 - Bad Request Error)

blobには、1行のテキスト(文字列)しか含まれていません。 BLOBは、POSTを介してテキストを受け取り、ユーザー指定の名前のBLOBにテキストを保存するAzure関数HttpTrigger(C#)を介してテキストで埋められます。ブロブを保存するとき、名前はすべて小文字に変換されます。

ユーザーはシンプルなHTML Webページにアクセスして、フォームにBLOBの名前を入力できます。ユーザーがHTMLフォームで[Submit]をクリックすると、別のAzure関数APIに対してPOSTが実行されます。この関数はブロブにアクセスし、ブロブからテキストを読み込みます。私がAzure関数の中から関数をテストするか、Postmanを使うと、正しく動作します(私はBLOBテキストを戻します)。

Webページを使用してAPIとやりとりしようとすると、Azure関数がBLOBから読み取るときに「400-Bad Request」が表示されます。ポストマンから正しく動作

APIのログ:詳細は以下をご参照ください

2017-04-11T20:19:14.340機能(ID = ea82f5c6-4345-40cc-90a5-1cb1cad78b7b)を開始した

2017-04-11T20:19:14.340 C#HTTPトリガー関数が要求を処理しました。

2017-04-11T20:19:POSTから14.340データ:blobName = TestBlob1submit = SubmitButtonText

2017-04-11T20:19:14.340ブロブ名は:testblob1

2017-04-11T20: 19:14.340 Azureストレージアカウントへのアクセス。

2017-04-11T20:19:14.402 Blobのテキスト:Hello world test!

2017-04-11T20:19:14.402完了機能(成功、ID = ea82f5c6-4345-40cc-90a5-1cb1cad78b7b)

APIのログがHTMLフォーム経由で動作しない:

2017-04-11T20:19:52.594関数が開始しました(Id = 1b1a39b6-0ab8-4673-bbf0-ae0006f7f7cf)

2017-04-11T20:19:52.594 C#HTTPトリガー関数が要求を処理しました。

2017-04-11T20:19:POSTから52.594データ:blobName = TestBlob1

ブロブテキスト

2017-04-11T20を提出=取得:19:52.594ブロブ名は次のとおりです。testblob1

2017-04-11T20:19:52.594 Azureストレージアカウントへのアクセス。

2017-04-11T20:19:52.626終了機能(障害、ID = 1b1a39b6-0ab8-4673-bbf0-ae0006f7f7cf)

2017-04-11T20:19:52.672例外機能の実行中:関数.Austin-SteelThread-HttpTrigger-DisplayBlobText。 Microsoft.WindowsAzure.Storage:リモートサーバーからエラーが返されました:(400)Bad Request。今私はちょうど得る(functionがBLOBのテキストを読み込み、Webブラウザがブロブのテキストが表示されるように、私はこの問題を解決するにはどうすればよい

#r "Microsoft.WindowsAzure.Storage" 
using System; 
using System.IO; 
using System.Net; 
using System.Text; 
using Microsoft.Azure; 
using Microsoft.WindowsAzure.Storage; 
using Microsoft.WindowsAzure.Storage.Blob; 

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log, Binder binder) 
{ 
    log.Info("C# HTTP trigger function processed a request."); 

    // Get text passed in POST 
    string postData = await req.Content.ReadAsStringAsync(); 
    log.Info("Data from POST: " + postData); 

    // Format blobName string to remove unwanted text 
    // Help from http://stackoverflow.com/questions/9505400/extract-part-of-a-string-between-point-a-and-b 
    int startPos = postData.LastIndexOf("blobName=") + "blobName=".Length; 
    int length = postData.IndexOf("submit=") - startPos; 
    string blobName = postData.Substring(startPos, length); 
    blobName = blobName.ToLower();  // Name of blob must be all lower-case 

    log.Info("Blob name is: " + blobName); 

    // START BLOB READING 
    log.Info("Accessing Azure Storage account."); 
    string containerAndBlob = "usertext-container/blob-" + blobName; 

    var attributes = new Attribute[] 
    { 
     new StorageAccountAttribute("[StorageAccountName]"), 
     new BlobAttribute(containerAndBlob) 
    }; 

    try 
    { 
     userBlobText = await binder.BindAsync<string>(attributes); 
    } 
    catch (StorageException ex) 
    { 
     var requestInformation = ex.RequestInformation; 
     var extendedInformation = requestInformation.ExtendedErrorInformation; 

     if (extendedInformation == null) 
     { 
      log.Info("No Extended Error Information!"); 
      log.Info(requestInformation.HttpStatusMessage); 
     } 
     else 
     { 
         log.Info(requestInformation.HttpStatusMessage); 

      var errorMessage = string.Format("({0}) {1}", extendedInformation.ErrorCode, extendedInformation.ErrorMessage); 

      var errorDetails = extendedInformation.AdditionalDetails.Aggregate("", (s, pair) => 
      { 
       return s + string.Format("{0}={1},", pair.Key, pair.Value); 
      }); 

      log.Info(errorMessage + ": Error Details: " + errorDetails); 
     } 
    } 

    log.Info("Text in Blob: " + userBlobText.ToString()); 
    // END BLOB READING 

    return userBlobText == null 
     ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass blob name in the request body.") 
     : req.CreateResponse(HttpStatusCode.OK, "Your blob stored the text: " + userBlobText.ToString()); 
} 

:ここ

は、問題のAzureの機能です空の文字列)?前もって感謝します。

+1

実際に例外をStorageExceptionとしてキャストする必要があります。次に、400エラーの詳細を表示することができます。私は例外のRequestInformationプロパティを調べることをお勧めします。 HTH。 –

+0

私はここに記載されているようにExtendedInformationを実装しようとしました[リンク](https://alexandrebrisebois.wordpress.com/2013/07/03/handling-windows-azure-storage-exceptions/)、私のエラーの拡張情報はありません。ちょうど400 - 悪い要求。 – TechAust10

答えて

1

Blobストレージに手動で接続する代わりに、バインディングエンジンを利用する必要があります。 binderパラメーターを関数に追加し、ファイルを取得するために使用します。

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, 
    TraceWriter log, Binder binder) 
{ 
    // Do await, not .Result 
    string postData = await req.Content.ReadAsStringAsync(); 

    // ... get your HTTP parameters here 

    var attributes = new Attribute[] 
    { 
     new StorageAccountAttribute(accountName), 
     new BlobAttribute(blobName) // blobName should have "container/blob" format 
    }; 

    var userBlobText = await binder.BindAsync<string>(attributes); 
    // do whatever you want with this blob... 
} 
+0

バインダーであなたの提案を実装すると、私はこのエラーを受け取ります: 'エラーCS1503:引数1: 'System.Attribute []'から 'System.Attribute''に変換できません – TechAust10

+1

' IBinder'を 'Binder'に変更しました – Mikhail

+0

属性の問題を修正するために働いた。 ''string':usingステートメントで使用される型は、暗黙的に 'System.IDisposable'に変換可能でなければなりません。 – TechAust10

関連する問題