Azure Functions

Serverless compute for the cloud

Table Storage Input Bindings

Table storage input bindings allow you to read data from an Azure Table Storage table directly into your Azure Function. This is a convenient way to access structured NoSQL data without writing explicit SDK code.

Supported Types

You can bind to the following types when using a Table Storage input binding:

Configuration

Table storage input bindings are configured in the function.json file. The primary parameters are:

Example function.json

{
  "bindings": [
    {
      "name": "product",
      "type": "table",
      "tableName": "Products",
      "partitionKey": "{category}",
      "rowKey": "{productId}",
      "connection": "AzureWebJobsStorage",
      "direction": "in"
    },
    {
      "name": "outputQueueItem",
      "type": "queue",
      "queueName": "output-messages",
      "connection": "AzureWebJobsStorage",
      "direction": "out"
    }
  ],
  "disabled": false
}

C# Example

This example shows a C# function that reads a product from Table Storage based on route parameters (e.g., /api/products/{category}/{productId}).

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

public static class GetProduct
{
    public static class Product
    {
        public string PartitionKey { get; set; }
        public string RowKey { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
    }

    [FunctionName("GetProduct")]
    public static void Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "products/{category}/{productId}")] HttpRequest req,
        string category,
        string productId,
        [Table("Products", partitionKey = "{category}", rowKey = "{productId}", Connection = "AzureWebJobsStorage")] Product product,
        ILogger log)
    {
        if (product == null)
        {
            log.LogWarning($"Product not found: Category={category}, ProductId={productId}");
            // Handle not found case, e.g., return a 404
            return;
        }

        log.LogInformation($"Retrieved product: {product.Name}");
        // Process the product data
        // ...
    }
}

PowerShell Example

A PowerShell function reading a single entity.

param($req, $log)

$category = $req.Query.category
$productId = $req.Query.productId

# Assuming 'product' is defined in function.json as an input binding
# Example function.json:
# {
#   "bindings": [
#     {
#       "name": "product",
#       "type": "table",
#       "tableName": "Products",
#       "partitionKey": "{category}",
#       "rowKey": "{productId}",
#       "connection": "AzureWebJobsStorage",
#       "direction": "in"
#     }
#   ], ...
# }

if ($product) {
    $log.LogInformation("Retrieved product: $($product.Name)")
    # Output the product details
    Push-OutputBinding -Name resp -Value ([HttpResponseContext]@{
        StatusCode = 200
        Body = $product | ConvertTo-Json
    })
} else {
    $log.LogWarning("Product not found.")
    Push-OutputBinding -Name resp -Value ([HttpResponseContext]@{
        StatusCode = 404
        Body = "Product not found"
    })
}

Important Considerations:

  • Ensure your connection string is correctly configured in your application settings.
  • The partitionKey and rowKey are fundamental to uniquely identifying an entity in Table Storage.
  • For complex queries beyond simple key lookups, consider using the filter parameter or the CloudTable binding type with the Table Storage SDK.
  • Performance can be optimized by designing your tables with appropriate partition keys.