如何在Azure Functions参数中调用动态值

问题描述

我有一个Azure函数,该函数会在服务总线主题上触发并将其从Blob存储复制数据到Azure Cosmos Db Table Api。根据Azure Blob存储中的文件名,文件数据将作为实体复制到Cosmos Db表Api中的相应表中。

问题:对于要动态复制的每个文件,我想在代码中引用表名,如果表不存在,那么我将在cosmos db中创建一个

例如,我在Azure函数中有以下代码

public static async void Run([ServiceBusTrigger("SBTopic","SBSubscription",Connection = "AzureServiceBusstring")] string mySbMsg,[CosmosDB(
databaseName: "dbname",collectionName: "{Dynamic table name}",ConnectionStringSetting = "CosmosDBConnection")] DocumentClient client,ILogger log)
{

    try {
         log.Loginformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
         // I get the variable name which will be my table name. Now refrencing this table name in Cosmos Db

         CloudTable table = client.GetTableReference(client);
         if (await table.CreateIfNotExistsAsync(Dynamic table name)) 
            { log("Created Table named: {0}",Dynamic table name); 
               // start copying entity 
            }
         else
            { log("Table {0} already exists",Dynamic table name); 
              // start copying entity 
            }.

在此动态表名称中,该名称将来自以相同方法运行的另一组代码。所以现在的问题是,我该如何首先处理Cosmos Db Connection,以及如何在代码中引用它。

谢谢

解决方法

您可以检查:http://dontcodetired.com/blog/post/Dynamic-Binding-in-Azure-Functions-with-Imperative-Runtime-Bindings并尝试使用cosmos db绑定进行类似的操作。

 BlobAttribute dynamicBlobBinding = new BlobAttribute(blobPath: $"{path details}/{dynamic property 1}/{dynamic property2}");

您还可以参考https://docs.microsoft.com/en-us/azure/azure-app-configuration/enable-dynamic-configuration-azure-functions-csharp并检查这在这种情况下是否有帮助。