适用于 Azure Functions 的 Azure 表存储输出绑定Azure Table storage output bindings for Azure Functions
使用 Azure 表存储输出绑定读取将实体写入 Azure 存储帐户中的表。Use an Azure Table storage output binding to write entities to a table in an Azure Storage account.
备注
此输出绑定不支持更新现有实体。This output binding does not support updating existing entities. 请使用 Azure 存储 SDK 中的 TableOperation.Replace
操作来更新现有实体。Use the TableOperation.Replace
operation from the Azure Storage SDK to update an existing entity.
示例Example
以下示例演示使用 HTTP 触发器写入单个表行的 C# 函数。The following example shows a C# function that uses an HTTP trigger to write a single table row.
public class TableStorage
{
public class MyPoco
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public string Text { get; set; }
}
[FunctionName("TableOutput")]
[return: Table("MyTable")]
public static MyPoco TableOutput([HttpTrigger] dynamic input, ILogger log)
{
log.LogInformation($"C# http trigger function processed: {input.Text}");
return new MyPoco { PartitionKey = "Http", RowKey = Guid.NewGuid().ToString(), Text = input.Text };
}
}
特性和注释Attributes and annotations
在 C# 类库中,使用 TableAttribute。In C# class libraries, use the TableAttribute.
该特性的构造函数采用表名称。The attribute's constructor takes the table name. 可对函数的 out
参数或返回值使用该特性,如以下示例中所示:The attribute can be used on an out
parameter or on the return value of the function, as shown in the following example:
[FunctionName("TableOutput")]
[return: Table("MyTable")]
public static MyPoco TableOutput(
[HttpTrigger] dynamic input,
ILogger log)
{
...
}
可以设置 Connection
属性来指定要使用的存储帐户,如以下示例中所示:You can set the Connection
property to specify the storage account to use, as shown in the following example:
[FunctionName("TableOutput")]
[return: Table("MyTable", Connection = "StorageConnectionAppSetting")]
public static MyPoco TableOutput(
[HttpTrigger] dynamic input,
ILogger log)
{
...
}
有关完整示例,请参阅 C# 示例。For a complete example, see the C# example.
可以使用 StorageAccount
特性在类、方法或参数级别指定存储帐户。You can use the StorageAccount
attribute to specify the storage account at class, method, or parameter level. 有关详细信息,请参阅输入 - 特性。For more information, see Input - attributes.
配置Configuration
下表解释了在 function.json 文件和 Table
特性中设置的绑定配置属性。The following table explains the binding configuration properties that you set in the function.json file and the Table
attribute.
function.json 属性function.json property | Attribute 属性Attribute property | 说明Description |
---|---|---|
type type | 不适用n/a | 必须设置为 table 。Must be set to table . 在 Azure 门户中创建绑定时,会自动设置此属性。This property is set automatically when you create the binding in the Azure portal. |
directiondirection | 不适用n/a | 必须设置为 out 。Must be set to out . 在 Azure 门户中创建绑定时,会自动设置此属性。This property is set automatically when you create the binding in the Azure portal. |
namename | 不适用n/a | 在函数代码中使用的、表示表或实体的变量名称。The variable name used in function code that represents the table or entity. 设置为 $return 可引用函数返回值。Set to $return to reference the function return value. |
tableNametableName | TableNameTableName | 表的名称。The name of the table. |
partitionKeypartitionKey | PartitionKeyPartitionKey | 要写入的表实体的分区键。The partition key of the table entity to write. 有关如何使用此属性的指导,请参阅用法部分。See the usage section for guidance on how to use this property. |
rowKeyrowKey | RowKeyRowKey | 要写入的表实体的行键。The row key of the table entity to write. 有关如何使用此属性的指导,请参阅用法部分。See the usage section for guidance on how to use this property. |
连接connection | ConnectionConnection | 包含要用于此绑定的存储连接字符串的应用设置的名称。The name of an app setting that contains the Storage connection string to use for this binding. 如果应用设置名称以“AzureWebJobs”开始,则只能在此处指定该名称的余下部分。If the app setting name begins with "AzureWebJobs", you can specify only the remainder of the name here. 例如,如果将 connection 设置为“MyStorage”,Functions 运行时将会查找名为“MyStorage”的应用设置。For example, if you set connection to "MyStorage", the Functions runtime looks for an app setting that is named "MyStorage". 如果将 connection 留空,函数运行时将使用名为 AzureWebJobsStorage 的应用设置中的默认存储连接字符串。If you leave connection empty, the Functions runtime uses the default Storage connection string in the app setting that is named AzureWebJobsStorage . |
在本地进行开发时,应用设置将取 local.settings.json 文件的值。When you're developing locally, app settings go into the local.settings.json file.
使用情况Usage
可以使用方法参数 ICollector<T> paramName
或 IAsyncCollector<T> paramName
访问输出表实体,其中 T
包括 PartitionKey
和 RowKey
属性。Access the output table entity by using a method parameter ICollector<T> paramName
or IAsyncCollector<T> paramName
where T
includes the PartitionKey
and RowKey
properties. 实现 ITableEntity
或继承 TableEntity
时通常会伴随使用这些属性。These properties are often accompanied by implementing ITableEntity
or inheriting TableEntity
.
另外,还可以使用 CloudTable
方法参数通过 Azure 存储 SDK 来写入到表。Alternatively you can use a CloudTable
method parameter to write to the table by using the Azure Storage SDK. 如果在尝试绑定到 CloudTable
时出现错误消息,请确保引用正确的存储 SDK 版本。If you try to bind to CloudTable
and get an error message, make sure that you have a reference to the correct Storage SDK version.
异常和返回代码Exceptions and return codes
绑定Binding | 参考Reference |
---|---|
表Table | 表错误代码Table Error Codes |
Blob、表、队列Blob, Table, Queue | 存储错误代码Storage Error Codes |
Blob、表、队列Blob, Table, Queue | 故障排除Troubleshooting |