尽管在这里或其他地方有很多帖子,我仍然没有找到如何从azure函数读取blob存储的信息。
我有以下内容
上述每个容器都有一个json文件“ customer.json”
现在,我需要调用函数并传递一个参数(例如“ london”)来检索伦敦客户
Customer customer= await azureFunctionService.GetCustomer(“London”);
该函数应该是什么样子,理想情况下,我想使用输入绑定从函数读取json文件,但是其他任何方式也都可以。
[FunctionName("GetCustomer")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
string inputBlobPath,
[Blob("howDoIBuildPathIncludingtheparameter",
FileAccess.Read, Connection = "WhereDoIGetThis")] string json,
ILogger log)
{
// Not sure if anything is required here apart from logging when using input binding
//
}
有什么建议吗?
非常感谢
答案 0 :(得分:2)
此 Microsoft 文档提供了一个简要示例,说明如何从 HttpTrigger
中提取数据以填充 blob 的输入绑定路径:https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-expressions-patterns#json-payloads
如上所述,单独定义负载对象,然后将此类型与 HttpTrigger
属性一起使用。然后对象属性可在其他输入绑定表达式中引用。
public class BlobInfo
{
public string CityName { get; set; }
}
public static class GetCustomer
{
[FunctionName("GetCustomer")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] BlobInfo info,
[Blob("{CityName}/customer.json", FileAccess.Read)] Stream blob,
ILogger log)
{
使用指定所需城市名称的 JSON 负载调用此函数,例如curl http://localhost:7071/api/GetCustomer -d "{'CityName':'manchester'}"
。
这将使用名为“manchester”的容器中名为“customer.json”的 blob 内容初始化 blob 输入参数。
答案 1 :(得分:0)
您是否尝试过以下代码。 以下示例是一个C#函数,该函数使用队列触发器和输入Blob绑定。队列消息包含Blob的名称,该函数记录Blob的大小。
[FunctionName("BlobInput")]
public static void Run(
[QueueTrigger("myqueue-items")] string myQueueItem,
[Blob("samples-workitems/{queueTrigger}", FileAccess.Read)] Stream myBlob,
ILogger log)
{
log.LogInformation($"BlobInput processed blob\n Name:{myQueueItem} \n Size: {myBlob.Length} bytes");
}