Table Storage Bindings
Table storage is a NoSQL key-attribute store that you can use to store large amounts of structured, non-relational data. Azure Functions provides input and output bindings for interacting with Azure Table Storage, simplifying the process of reading from and writing to tables.
Supported Operations
The table storage bindings for Azure Functions enable you to:
- Input Binding: Retrieve a single entity or a collection of entities from a table based on a partition key and row key, or a query.
- Output Binding: Insert or update entities in an Azure Table Storage table.
Configuration
To use the table storage bindings, you need to define them in your function.json file (for JavaScript, Python, and other non-.NET languages) or via attributes (for C#/.NET).
function.json Example (JavaScript)
{
"scriptFile": "index.js",
"bindings": [
{
"name": "inputTable",
"type": "table",
"direction": "in",
"tableName": "products",
"partitionKey": "{partitionKey}",
"rowKey": "{rowKey}",
"connection": "AzureWebJobsStorage"
},
{
"name": "outputTable",
"type": "table",
"direction": "out",
"tableName": "products",
"connection": "AzureWebJobsStorage"
},
{
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": [
"get",
"post"
]
},
{
"name": "res",
"type": "http",
"direction": "out"
}
]
}
Input Binding Details
The input binding allows you to fetch data from a table. You can specify the tableName, partitionKey, and rowKey. If you omit rowKey, the binding attempts to fetch all entities for the given partitionKey.
Properties:
name: The name of the variable that will hold the retrieved entity or entities in your function code.type: Must betable.direction: Must bein.tableName: The name of the table to read from.partitionKey: The partition key to filter by. Can be a literal string or a dynamic value from the trigger or other input bindings (e.g.,"{queueTrigger}").rowKey: The row key to filter by. Can be a literal string or a dynamic value. If omitted, all entities for the partition key are returned.connection: The name of the app setting that contains the Azure Storage connection string. Defaults toAzureWebJobsStorage.
rowKey to retrieve multiple entities, the function parameter should be of a collection type (e.g., an array or list).
Output Binding Details
The output binding is used to insert or update entities in a table. You typically use a collector object (like IAsyncCollector<TEntity> in C# or func.Out[func.TableEntity] in Python) to add entities.
Properties:
name: The name of the variable used to interact with the output binding in your function code.type: Must betable.direction: Must beout.tableName: The name of the table to write to.connection: The name of the app setting that contains the Azure Storage connection string. Defaults toAzureWebJobsStorage.
Entity Structure
Entities in Azure Table Storage consist of a PartitionKey, a RowKey, and a set of named properties. The PartitionKey and RowKey together form the unique identifier for an entity.
| Property | Type | Description |
|---|---|---|
PartitionKey |
String | Required. Used for partitioning data. |
RowKey |
String | Required. Unique within a partition. |
Timestamp |
DateTime | Automatically managed by Azure Table Storage. |
| Custom Properties | Various (String, Int, Double, Boolean, DateTime, Guid, Binary) | User-defined data properties. |
Example Usage
Here's a conceptual example of how you might use the bindings to retrieve a product by its ID and then update its price:
// index.js (simplified)
module.exports = async function (context, req, inputTable) {
context.log('JavaScript HTTP trigger function processed a request.');
const partitionKey = req.query.partitionKey || req.body.partitionKey;
const rowKey = req.query.rowKey || req.body.rowKey;
const newPrice = req.body.price;
if (!partitionKey || !rowKey) {
context.res = { status: 400, body: "Please provide partitionKey and rowKey." };
return;
}
// inputTable contains the entity if found, or null
if (inputTable) {
context.log(`Found product: ${inputTable.name} - $${inputTable.price}`);
if (newPrice !== undefined) {
// Using output binding to update
context.bindings.outputTable = {
PartitionKey: partitionKey,
RowKey: rowKey,
name: inputTable.name, // Keep existing name
price: newPrice
};
context.res = { body: "Product price updated successfully." };
} else {
context.res = { body: inputTable };
}
} else {
context.res = { status: 404, body: "Product not found." };
}
};
Troubleshooting
- Ensure your
connectionstring inappsettings.jsonor environment variables is correct and points to a valid Azure Storage account. - Verify that the
tableNamespecified in your binding configuration exists in your storage account. - Check that the
partitionKeyandrowKeyyou are using are valid and exist in your table. - For dynamic keys (e.g., from HTTP parameters or queue messages), ensure the parameter names match exactly.