Azure:无法从Azure函数HttpTrigger读取Blob文本(400 - 错误请求错误)

时间:2017-04-11 17:46:41

标签: c# azure http-post azure-functions

我无法在Azure(Blob)存储上读取存储在Blob中的文本。

blob只包含一行文本(一个字符串)。 blob通过Azure Functions HttpTrigger(C#)填充文本,该函数通过POST接收文本并将文本保存到具有用户指定名称的blob。保存blob时,名称将转换为全小写。

然后,用户可以访问简单的HTML网页,并在表单中输入blob的名称。当用户点击"提交"在HTML表单上,针对不同的Azure Function API执行POST。此函数访问blob并从中读取文本。每当我从Azure函数或使用Postman测试函数时,它都能正常工作(我得到blob文本)。

当我尝试使用网页与API进行交互时,我得到了一个" 400 - 错误请求"当Azure函数从blob中读取时。请参阅以下详细信息:

API日志从Postman正确运行:

2017-04-11T20:19:14.340功能开始(Id = ea82f5c6-4345-40cc-90a5-1cb1cad78b7b)

2017-04-11T20:19:14.340 C#HTTP触发函数处理了一个请求。

2017-04-11T20:19:14.340来自POST的数据: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:52.594 POST数据:blobName = TestBlob1

submit =检索Blob文本

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执行函数时出现异常:Functions.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文本,Web浏览器显示blob的文本(现在我只得到一个空字符串)?提前谢谢。

1 个答案:

答案 0 :(得分: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...
}