.NET 用 Azure WebJobs Tables クライアント ライブラリ - バージョン 1.2.0

この拡張機能は、Azure Functionsの Azure Tables にアクセスするための機能を提供します。

作業の開始

パッケージをインストールする

NuGet を使用して Tables 拡張機能をインストールします。

dotnet add package Microsoft.Azure.WebJobs.Extensions.Tables

前提条件

このパッケージを使用するには、 Azure サブスクリプションストレージ アカウント または Cosmos Tables アカウント が必要です。

ストレージ テーブルの使用

新しいストレージ アカウントを作成するには、Azure PortalAzure PowerShell、または Azure CLI を使用できます。 Azure CLI を使う例を次に示します。

az storage account create --name <your-resource-name> --resource-group <your-resource-group-name> --location westus --sku Standard_LRS

Cosmos テーブルの使用

新しい Cosmos テーブルを作成するには、Azure PortalAzure PowerShell、または Azure CLI を使用できます。

クライアントを認証する

接続は、テーブル サービスに接続するために必要な情報のセットを表します。 接続文字列、エンドポイント、トークン資格情報、または共有キーを含めることができます。

TableAttribute プロパティはConnection、Table Service アクセスに使用される接続を定義します。 たとえば、 [Tables(Connection="MyTableService")] は接続を使用 MyTableService します。

接続情報は、 または Azure portal のアプリケーション設定で設定local.settings.jsonできます。

local.settings.json に設定を追加する場合は、 プロパティの下にValues配置します。

{
  "IsEncrypted": false,
  "Values": {
    "MyTableService": "..."
  }
}

Azure portalのアプリケーション設定に設定を追加する場合は、指定された名前を直接使用します。

MyTableService = ...

テーブル拡張機能では、既定で接続名が AzureWebJobsStorage 使用されます。

接続文字列

接続文字列認証を使用するには、接続文字列の値を接続設定に直接割り当てます。

<ConnectionName> = DefaultEndpointsProtocol=https;AccountName=...;AccountKey=...;EndpointSuffix=core.windows.net

エンドポイントとトークンの資格情報の使用

注: トークン資格情報認証は、ストレージ テーブルでのみサポートされます。

<ConnectionName>__endpoint = https://...table.core.windows.net

資格情報が指定されていない場合は、 DefaultAzureCredential が使用されます。

ユーザー割り当て管理 ID を使用する場合は、 と credential の設定をclientId指定する必要があります。

<ConnectionName>__credential = managedidentity

<ConnectionName>__clientId = <user-assigned client id>

共有キー資格情報の使用

共有キー認証を使用する場合は、 endpointaccountKeyaccountName を指定する必要があります。

<ConnectionName>__endpoint = https://...table.core.windows.net

<ConnectionName>__credential__accountName = <account name>

<ConnectionName>__credential__accountKey = <account key>

主要な概念

入力バインドを使用すると、Azure 関数への入力としてテーブルを読み取ることができます。 出力バインドを使用すると、Azure 関数のテーブル行を変更および削除できます。

この拡張機能を使用して Table Service にアクセスする方法については、入力バインドのチュートリアルと出力バインドのチュートリアルに従ってください。

テーブル拡張機能では、バインディングのみが提供されます。 バインド自体で関数をトリガーすることはできません。 テーブルへのエントリの読み取りまたは書き込みのみが可能です。

次の例では、 HTTP トリガー を使用して関数を呼び出します。

1 つのエンティティへのバインド

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"]}");
        }
    }
}

1 つのエンティティの作成

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"
        };
    }
}

モデルを使用して 1 つのエンティティを作成する

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()
            });
        }
    }
}

SDK TableClient 型へのバインド

TableClient メソッド パラメーターを使用して、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()
        });
    }
}

トラブルシューティング

トラブルシューティングのガイダンスについては、「監視Azure Functions」を参照してください。

次のステップ

Azure 関数の概要または Azure 関数の作成に関するガイドを参照してください

共同作成

このライブラリのビルド、テスト、および投稿の詳細については、 CONTRIBUTING.md を参照してください。

このプロジェクトでは、共同作成と提案を歓迎しています。 ほとんどの共同作成では、共同作成者使用許諾契約書 (CLA) にご同意いただき、ご自身の共同作成内容を使用する権利を Microsoft に供与する権利をお持ちであり、かつ実際に供与することを宣言していただく必要があります。 詳細については、「 cla.microsoft.com」を参照してください。

このプロジェクトでは、Microsoft オープン ソースの倫理規定を採用しています。 詳しくは、「Code of Conduct FAQ (倫理規定についてよくある質問)」を参照するか、opencode@microsoft.com 宛てに質問またはコメントをお送りください。

インプレッション数