skip to Main Content

I have an azure function that is running on a timer. I want to write the last time the function ran successfully to table storage in the same storage account the function is using.

Is it possible to get the connection string for a function’s storage account with c# and read/write to it?

2

Answers


  1. Is it possible to get the connection string for a function’s storage account and read/write to it?

    Yes, You can get by going to configuration section of function app and in that select AzureWebJobsStorage.

    enter image description here

    The above selected is the connection string.

    Alternatively, Firstly, you need to identify the storage account connected and use below process:

    Open that Storage Account and get connection like below:

    You need to got to Access keys section and get the connection string.

    enter image description here

    Yes, there are so many C#, python and other language SDKs with which we can easily access storage accounts . (You can use Output Bindings and in C# you can give it in local settings)

    Login or Signup to reply.
  2. Yes, follow these steps to get the connection string from your function app’s storage account

    Then, use Table Storage as an output binding, according to the programming language you are using the process of adding the connection string may vary, but in most of the cases you will need to paste it at the configuration file.

    For C#

    While the attribute takes a Connection property, you can also use the StorageAccountAttribute to specify a storage account connection. You can do this when you need to use a different storage account than other functions in the library. The constructor takes the name of an app setting that contains a storage connection string. The attribute can be applied at the parameter, method, or class level. The following example shows class level and method level:

    [StorageAccount("ClassLevelStorageAppSetting")]
    public static class AzureFunctions
    {
        [FunctionName("StorageTrigger")]
        [StorageAccount("FunctionLevelStorageAppSetting")]
        public static void Run( //...
    {
        ...
    }
    

    and within local.settings.json define and paste the value at the app setting

    {
        "IsEncrypted": false,
        "Values": {
            "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=3mpO1cPf6DXl0At5Ddfew654few5BpF66B4aYd81Qdgpj3jjj5k5SU2VjdD2fewKpkwaw==;EndpointSuffix=core.windows.net",
            "FUNCTIONS_WORKER_RUNTIME": "dotnet"
        }
    }
    

    If the app setting name begins with "AzureWebJobs", you can specify only the remainder of the name here. For example, if you set connection to "MyStorage", the Functions runtime looks for an app setting that is named "AzureWebJobsMyStorage". If you leave connection empty, the Functions runtime uses the default Storage connection string in the app setting that is named AzureWebJobsStorage.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search