【问题标题】:How to define "external" StorageAccount in Azure Function?如何在 Azure Function 中定义“外部”StorageAccount?
【发布时间】:2020-02-26 20:49:45
【问题描述】:

所以我正在编写一个小的 Azure 函数来计算 StorageAccount 表中唯一实体的总数。到目前为止,一切正常,除了与存储表的连接。在开发过程中,我使用了函数的 StorageAccount,它运行良好。但现在我需要连接到不同的 StorageAccount 表。

这是我目前得到的:

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Onkeliroh.Test
{
    public static class TableStorageEntityCounter
    {
        [StorageAccount("fwetabletest")]
        [FunctionName("TableStorageEntityCounter")]
        public static async Task Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer,
                                     [Table("BlaBlub", Connection = "fwetabletest")]CloudTable cloudTable,
                                     ILogger log)
        {
            var totalCount = await GetCountOfEntitiesInPartition(cloudTable);

            log.LogInformation("Total Entity Count:" + totalCount.ToString());
        }

        public static async Task<int> GetCountOfEntitiesInPartition(CloudTable table)
        { \\[...]
        }
    }
}

我位于同一个 ResourceGroup 中的“外部”StorageAccount。

我的问题是:如何告诉我的函数使用另一个 StorageAccount? StorageAccount 装饰器——显然——不起作用。

【问题讨论】:

  • 您应该有一个 AppSetting "fwetabletest",其中包含到您的外部存储的连接字符串。这一切都应该工作

标签: c# azure azure-functions azure-storage azure-table-storage


【解决方案1】:

TableAttribute 中的 Connection 参数告诉您的绑定要从设置文件(本地)或应用设置(在 Azure 中)获取哪个存储连接字符串设置。

所以:[Table("BlaBlub", Connection = "fwetabletest")] 表示您告诉绑定连接字符串设置的名称是 fwetabletest。确保有一个名为 fwetabletest 的连接字符串设置指向函数应用配置下的“外部”存储帐户,您应该一切顺利。

【讨论】:

    【解决方案2】:

    您实际上应该在配置级别定义它并相应地创建客户端,您可以在 settings.json 文件中拥有连接字符串并访问它

       private static readonly string BLOB_STORAGE_CONNECTION_STRING = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
    

    SAMPLE CODE

    【讨论】:

    • 如何在配置级别定义它?据我所知,C# Azure Functions 在构建过程中被解析,我没有机会自己编辑function.json。
    • 您可以通过转到您的函数然后连接字符串在门户上进行设置
    猜你喜欢
    • 2021-11-20
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    相关资源
    最近更新 更多