This package extends the Microsoft.Azure.WebJobs library with Table triggers using Azure Table service.
$ dotnet add package Microsoft.Azure.WebJobs.Extensions.TablesThis extension provides functionality for accessing Azure Tables in Azure Functions.
Install the Tables extension with NuGet:
dotnet add package Microsoft.Azure.WebJobs.Extensions.Tables
You need an Azure subscription and a Storage Account or Cosmos Tables Account to use this package.
To create a new Storage Account, you can use the Azure Portal, Azure PowerShell, or the Azure CLI. Here's an example using the Azure CLI:
az storage account create --name <your-resource-name> --resource-group <your-resource-group-name> --location westus --sku Standard_LRS
To create a new Cosmos Tables , you can use the Azure Portal, Azure PowerShell, or the Azure CLI.
Connection represents a set of information required to connect to a table service. It can contain a connection string, an endpoint, token credential or a shared key.
The Connection property of TableAttribute defines which connection is used for the Table Service access. For example, is going to use connection.
[Tables(Connection="MyTableService")]MyTableServiceThe connection information can be set in local.settings.json or application settings in Azure portal.
When adding a setting to local.settings.json place it under the Values property:
{
"IsEncrypted": false,
"Values": {
"MyTableService": "..."
}
}
When adding a setting to application settings in Azure portal use the provided name directly:
MyTableService = ...
Tables extension uses the AzureWebJobsStorage connection name by default.
To use connection strings authentication assign connection string value directly to the connection setting.
<ConnectionName> = DefaultEndpointsProtocol=https;AccountName=...;AccountKey=...;EndpointSuffix=core.windows.net
NOTE: token credential authentication is supported only for storage tables.
<ConnectionName>__endpoint = https://...table.core.windows.net
If no credential information is provided the DefaultAzureCredential is used.
When using user-assigned manageed identity the clientId and credential settings need to be provided:
<ConnectionName>__credential = managedidentity
<ConnectionName>__clientId = <user-assigned client id>
When using shared key authentication the endpoint, accountKey and accountName need to be provided.
<ConnectionName>__endpoint = https://...table.core.windows.net
<ConnectionName>__credential__accountName = <account name>
<ConnectionName>__credential__accountKey = <account key>
The input binding allows you to read table as input to an Azure Function. The output binding allows you to modify and delete table rows in an Azure Function.
Please follow the input binding tutorial and output binding tutorial to learn about using this extension for accessing table service.
Tables extensions provides only bindings. Bindings by themselves can't trigger a function. It can only read or write entries to the table.
In the following example we use HTTP trigger to invoke the function.
public class InputSingle
{
[FunctionName("InputSingle")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "GET")] HttpRequest request,
[Table("MyTable", "<PartitionKey>", "<RowKey>")] TableEntity entity, ILogger log)
{
log.LogInformation($"PK={entity.PartitionKey}, RK={entity.RowKey}, Text={entity["Text"]}");
}
}
public class MyEntity
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string Text { get; set; }
}
public class InputSingleModel
{
[FunctionName("InputSingleModel")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "GET")] HttpRequest request,
[Table("MyTable", "<PartitionKey>", "<RowKey>")] MyEntity entity, ILogger log)
{
log.LogInformation($"PK={entity.PartitionKey}, RK={entity.RowKey}, Text={entity.Text}");
}
}
public class InputMultipleEntitiesFilter
{
[FunctionName("InputMultipleEntitiesFilter")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "GET")] HttpRequest request,
[Table("MyTable", "<PartitionKey>", Filter = "Text ne ''")] IEnumerable<TableEntity> entities, ILogger log)
{
foreach (var entity in entities)
{
log.LogInformation($"PK={entity.PartitionKey}, RK={entity.RowKey}, Text={entity["Text"]}");
}
}
}
public class OutputSingle
{
[FunctionName("OutputSingle")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "GET")] HttpRequest request,
[Table("MyTable")] out TableEntity entity)
{
entity = new TableEntity("<PartitionKey>", "<RowKey>")
{
["Text"] = "Hello"
};
}
}
public class MyEntity
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string Text { get; set; }
}
public class OutputSingleModel
{
[FunctionName("OutputSingleModel")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "GET")] HttpRequest request,
[Table("MyTable")] out MyEntity entity)
{
entity = new MyEntity()
{
PartitionKey = "<PartitionKey>",
RowKey = "<RowKey>",
Text = "Hello"
};
}
}
public class OutputMultiple
{
[FunctionName("OutputMultiple")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "POST")] HttpRequest request,
[Table("MyTable")] IAsyncCollector<TableEntity> collector)
{
for (int i = 0; i < 10; i++)
{
collector.AddAsync(new TableEntity("<PartitionKey>", i.ToString())
{
["Text"] = i.ToString()
});
}
}
}
public class MyEntity
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string Text { get; set; }
}
public class OutputMultipleModel
{
[FunctionName("OutputMultipleModel")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "POST")] HttpRequest request,
[Table("MyTable")] IAsyncCollector<MyEntity> collector)
{
for (int i = 0; i < 10; i++)
{
collector.AddAsync(new MyEntity()
{
PartitionKey = "<PartitionKey>",
RowKey = i.ToString(),
Text = i.ToString()
});
}
}
}
Use a TableClient method parameter to access the table by using the Azure Tables SDK.
public class BindTableClient
{
[FunctionName("BindTableClient")]
public static async Task Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "POST")] HttpRequest request,
[Table("MyTable")] TableClient client)
{
await client.AddEntityAsync(new TableEntity("<PartitionKey>", "<RowKey>")
{
["Text"] = request.GetEncodedPathAndQuery()
});
}
}
Please refer to Monitor Azure Functions for troubleshooting guidance.
See the CONTRIBUTING.md for details on building, testing, and contributing to this library.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.