c# - Azure函数从HttpTriggerFunction访问CloudBlobContainer

标签 c# azure azure-storage azure-functions

对 Azure Functions 非常非常陌生,并且感到非常沮丧。

我想要做的就是执行来自 HttpTriggerFunction 的“get”请求并从 CloudBlobContainer 返回流内容。

我真的不明白为什么这么难。只是尝试使用 Azure Functions 托管 SPA。

类似这样的

  public static class UIHandler
{
    [FunctionName("UIHandler")]
    public static async Task<HttpResponseMessage> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequest req, 
        TraceWriter log, 
        CloudBlobContainer container)
    {
        log.Info("C# HTTP trigger function processed a request.");
        var stream = await container.GetBlockBlobReference({Infer file name from request here}).OpenReadAsync();

        return new HttpResponseMessage()
        {
            StatusCode = HttpStatusCode.OK,
            Content = new StreamContent(stream)
        };

    }
}

当我尝试运行此程序时,出现以下错误。

Run: Microsoft.Azure.WebJobs.Host: Error indexing method 'UIHandler.Run'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'container' to type CloudBlobContainer. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).

我正在使用 Azure Functions 2。我无法从网上看到如何为此设置浏览扩展。我还研究了输入和输出绑定(bind)。我不明白当您使用似乎只存在于 JSON 中的 C# 时,是什么使参数输入或输出绑定(bind)。

我需要相应的 JSON 文件吗?如果是的话,它叫什么,它去哪里了。

提前致谢

最佳答案

看看Blob Storage Input Binding 。第一个示例展示了如何读取 blob 流,只需将队列触发器替换为 HTTP 触发器,例如

[FunctionName("UIHandler")]
public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", Route = "{name}")] HttpRequest req,
    string name, 
    TraceWriter log, 
    [Blob("samples-workitems/{name}", FileAccess.Read)] Stream stream)
{
    log.Info($"C# HTTP trigger function processed a request for {name}.");

    return new HttpResponseMessage()
    {
        StatusCode = HttpStatusCode.OK,
        Content = new StreamContent(stream)
    };

}

关于c# - Azure函数从HttpTriggerFunction访问CloudBlobContainer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51211876/

相关文章:

c# - 下载大型 Azure 存储 Blob 的最快方法?

c# - Azure 表存储位数据类型

azure - 如何获取Windows Azure Portal中的用户日志?

c# - 如何以编程方式设置 TARGETDIR?

c# - 如何通过 javascript 填充 asp 下拉列表?

c# - 为什么 double.TryParse() 在精度 > 16 时返回 true

azure - 如何配置【Azure】应用网关支持多种App服务?

c# - 在 MapReduce 中使用 Windows Azure 存储执行 Reduce 函数后无法看到最终结果

azure - 如何将多域用户电子邮件添加到 Azure AD

c# - 如何在 C# 中将 RepeatBehavior.Forever 转换为 2x?