使用Azure HTTP触发器函数(C#)返回存储在blob存储中的pdf



我正在使用C#和.NET6我有一个HTTP触发器函数,它使用Blob输入绑定从Blob中获取PDF文件存储
我对该函数的返回类型有问题
我已经知道我有blob内容,因为它的长度>0.

这是我的代码:

public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "foo/{bar}")] HttpRequest req,
[Blob("foo/{bar}.pdf", FileAccess.Read)] Stream blobContent,
ILogger log, string bar)
{
if (blobContent == null)
{
// TODO return error page:
return new OkResult();
}
else
{
// Return pdf from blob storage:
blobContent.Seek(0, SeekOrigin.Begin);
FileStreamResult fsr;
try
{
fsr = new FileStreamResult(blobContent, MediaTypeNames.Application.Pdf);
}
catch (Exception e)
{
log.LogError(e, "Error returning blobcontent");
throw;
}

return fsr;
}
}

调试时,我可以看到blobContent有内容
但当我把它运行到最后时,我在终端中得到了这个错误:

An unhandled host error has occurred.
System.Private.CoreLib: Value cannot be null. (Parameter 'buffer').

所以我没有正确处理blobContent
如何正确返回流?

这似乎太琐碎了,我甚至找不到这样简单的例子。

切换到byte[]作为输入,只使用FileContentResult

[FunctionName("Blob2Http")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "blob/{filename}")] HttpRequest req,
[Blob("test/{filename}.pdf", FileAccess.Read)] byte[] blobContent,
ILogger log, string filename)
{
if (blobContent == null)
{
// TODO return error page:
return new OkResult();
}
else
{
try
{
return new FileContentResult(blobContent, MediaTypeNames.Application.Pdf)
{
FileDownloadName = $"{filename}.pdf"
};
}
catch (Exception e)
{
log.LogError(e, "Error returning blobcontent");
throw;
}
}
}

最新更新