Search code examples
c#azureazure-functionsazure-storageazure-table-storage

How to define "external" StorageAccount in Azure Function?


So i am writing a small Azure Function to count the total amount of unique entities in a StorageAccount Table. So far everything works, except for the connection to the Storage Table. During the development i used the StorageAccount of the Function and it worked fine. But now i need to connect to a different StorageAccount Table.

This is what i got so far:

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)
        { \\[...]
        }
    }
}

The "external" StorageAccount i located in the same ResourceGroup.

My Question is: How to i tell my function to use the other StorageAccount? The StorageAccount decorator -- apparently -- isn't working.


Solution

  • The Connection parameter in the TableAttribute tells your binding which storage connection string setting to take from your settings file (locally) or the App Settings (in Azure).

    So this: [Table("BlaBlub", Connection = "fwetabletest")] means you're telling the binding the name of the connection string setting is fwetabletest. Make sure to have a connection string setting named fwetabletest pointing to the 'external' storage account under the Function App Configuration and you should be good to go.

    enter image description here