2017-04-11 64 views
0

我無法讀取存儲在Azure(Blob)存儲上的blob中的文本。Azure:無法從Azure函數讀取Blob文本HttpTrigger(400 - 錯誤請求錯誤)

blob只包含一行文本(字符串)。通過Azure函數HttpTrigger(C#)通過POST接收文本並使用用戶指定的名稱將文本保存到Blob中,從而使Blob充滿文本。保存blob時,名稱將全部轉換爲小寫。

用戶可以訪問一個簡單的HTML網頁,並將該BLOB的名稱輸入到表單中。當用戶在HTML表單上單擊「提交」時,將針對不同的Azure功能API執行POST。該函數訪問blob並從中讀取文本。每當我從Azure函數或使用Postman測試函數時,它都能正常工作(我得到blob文本)。

當我嘗試使用網頁與API進行交互時,當Azure函數從blob讀取時,會出現「400 - 錯誤請求」。請看下面的詳細信息:

2017-04-11T20:19:

登錄API從郵差正常工作的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 BLOB名稱是: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 BLOB名稱是: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)錯誤的請求。

這裏是有問題的Azure的功能:

#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()); 
} 

我怎樣才能解決這個問題,使該功能可以讀取BLOB文本和網頁瀏覽器顯示BLOB的文本(現在我得到的只是一個空字符串)?先謝謝你。

+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'爲'活頁夾' – Mikhail

+0

努力解決屬性問題。現在我得到錯誤:''字符串':在使用語句中使用的類型必須隱式轉換爲'System.IDisposable'' – TechAust10

相關問題