2017-04-11 66 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

相关问题