Error binding Blob to IAsyncCollector when binding to output blob in Async method

杀马特。学长 韩版系。学妹 提交于 2019-12-01 11:12:51

Collectors are not supported for Blob output bindings, see this issue.

For variable amount of output blobs (0 or 1 in your case, but can be any), you would have to use imperative bindings. Remove collection binding from your function.json and then do this:

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, Binder binder)
{
    if (req.Method == HttpMethod.Post) 
    {
        string jsonContent = await req.Content.ReadAsStringAsync();

        var attributes = new Attribute[]
        {    
            new BlobAttribute("testdata/{rand-guid}.txt"),
            new StorageAccountAttribute("test_STORAGE")
        };

        using (var writer = await binder.BindAsync<TextWriter>(attributes))
        {
            writer.Write(jsonContent);
        }

        return req.CreateResponse(HttpStatusCode.OK);
    }
    else 
    {
        return req.CreateResponse(HttpStatusCode.BadRequest);    
    }
}

You can use the Blob-Binding.

I preferred this way, because I'm able to specify the ContentType.

 [FunctionName(nameof(Store))]
    public static async Task<IActionResult> Store(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
        [Blob(
            "firstcontainer",
            FileAccess.Read,
            Connection = "blobConnection")] CloudBlobContainer blobContainer,
        ILogger log)
    {
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

        string filename = "nextlevel/body.json";

        CloudBlockBlob blob = blobContainer.GetBlockBlobReference($"{filename}");
        blob.Properties.ContentType = "application/json";
        await blob.UploadTextAsync(requestBody);

        return (ActionResult)new OkResult();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!