.NET 用 Azure WebJobs Storage BLOB クライアント ライブラリ - バージョン 5.2.1

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

作業の開始

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

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

dotnet add package Microsoft.Azure.WebJobs.Extensions.Storage.Blobs

前提条件

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

新しいストレージ アカウントを作成するには、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

クライアントを認証する

拡張機能が BLOB にアクセスするには、 Azure Portal または以下の Azure CLI スニペットを使用して見つかる接続文字列が必要です。

az storage account show-connection-string -g <your-resource-group-name> -n <your-resource-name>

接続文字列は、 AzureWebJobsStorage アプリ設定を使用して指定できます。

主要な概念

BLOB トリガーの使用

Blob ストレージ トリガーは、新しいまたは更新された BLOB が検出されたときに関数を開始します。 BLOB のコンテンツは、関数への入力として提供されます。

BLOB が変更されたときに Azure 関数をトリガーする方法については、 チュートリアル に従ってください。

リスニング戦略

BLOB トリガーには、BLOB の作成と変更のリッスンに関して、いくつかの戦略が用意されています。 戦略は、 の プロパティを Source 指定することでカスタマイズできます (以下の BlobTrigger 例を参照)。

既定の戦略

既定では、BLOB トリガーはポーリングを使用します。これは、Azure Storage 分析ログの検査と定期的なコンテナー スキャンの実行のハイブリッドとして機能します。 BLOB は、間隔の間で使用される継続トークンを使用して、一度に 10,000 のグループ単位でスキャンされます。

Azure Storage 分析ログ は既定では有効になっていません。有効にする方法については、「 Azure Storage 分析ログ」 を参照してください。

この戦略は、待機時間が短い大規模なアプリケーションやシナリオでは推奨されません。

イベント グリッド

BLOB ストレージ イベント を使用して、変更をリッスンできます。 この方法では 、追加のセットアップが必要です。

この戦略は、大規模なアプリケーションに推奨されます。

BLOB バインドの使用

入力バインドを使用すると、Azure 関数への入力として BLOB Storage データを読み取ることができます。 出力バインドを使用すると、Azure 関数内で Blob Storage データを変更および削除できます。

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

BLOB の変更に対応する

既定の戦略

public static class BlobFunction_ReactToBlobChange
{
    [FunctionName("BlobFunction")]
    public static void Run(
        [BlobTrigger("sample-container/sample-blob")] Stream blobStream,
        ILogger logger)
    {
        using var blobStreamReader = new StreamReader(blobStream);
        logger.LogInformation("Blob sample-container/sample-blob has been updated with content: {content}", blobStreamReader.ReadToEnd());
    }
}

Event Grid 戦略

public static class BlobFunction_ReactToBlobChange_EventGrid
{
    [FunctionName("BlobFunction")]
    public static void Run(
        [BlobTrigger("sample-container/sample-blob", Source = BlobTriggerSource.EventGrid)] Stream blobStream,
        ILogger logger)
    {
        using var blobStreamReader = new StreamReader(blobStream);
        logger.LogInformation("Blob sample-container/sample-blob has been updated with content: {content}", blobStreamReader.ReadToEnd());
    }
}

ストリームからの読み取り

public static class BlobFunction_ReadStream
{
    [FunctionName("BlobFunction")]
    public static void Run(
        [BlobTrigger("sample-container/sample-blob-1")] Stream blobStream1,
        [Blob("sample-container/sample-blob-2", FileAccess.Read)] Stream blobStream2,
        ILogger logger)
    {
        using var blobStreamReader1 = new StreamReader(blobStream1);
        logger.LogInformation("Blob sample-container/sample-blob-1 has been updated with content: {content}", blobStreamReader1.ReadToEnd());
        using var blobStreamReader2 = new StreamReader(blobStream2);
        logger.LogInformation("Blob sample-container/sample-blob-2 has content: {content}", blobStreamReader2.ReadToEnd());
    }
}

ストリームへの書き込み

public static class BlobFunction_WriteStream
{
    [FunctionName("BlobFunction")]
    public static async Task Run(
        [BlobTrigger("sample-container/sample-blob-1")] Stream blobStream1,
        [Blob("sample-container/sample-blob-2", FileAccess.Write)] Stream blobStream2,
        ILogger logger)
    {
        await blobStream1.CopyToAsync(blobStream2);
        logger.LogInformation("Blob sample-container/sample-blob-1 has been copied to sample-container/sample-blob-2");
    }
}

文字列へのバインド

public static class BlobFunction_String
{
    [FunctionName("BlobFunction")]
    public static void Run(
        [BlobTrigger("sample-container/sample-blob-1")] string blobContent1,
        [Blob("sample-container/sample-blob-2")] string blobContent2,
        ILogger logger)
    {
        logger.LogInformation("Blob sample-container/sample-blob-1 has been updated with content: {content}", blobContent1);
        logger.LogInformation("Blob sample-container/sample-blob-2 has content: {content}", blobContent2);
    }
}

BLOB への文字列の書き込み

public static class BlobFunction_String_Write
{
    [FunctionName("BlobFunction")]
    public static void Run(
        [BlobTrigger("sample-container/sample-blob-1")] string blobContent1,
        [Blob("sample-container/sample-blob-2")] out string blobContent2,
        ILogger logger)
    {
        logger.LogInformation("Blob sample-container/sample-blob-1 has been updated with content: {content}", blobContent1);
        blobContent2 = blobContent1;
        logger.LogInformation("Blob sample-container/sample-blob-1 has been copied to sample-container/sample-blob-2");
    }
}

バイト配列へのバインド

public static class BlobFunction_ByteArray
{
    [FunctionName("BlobFunction")]
    public static void Run(
        [BlobTrigger("sample-container/sample-blob-1")] byte[] blobContent1,
        [Blob("sample-container/sample-blob-2")] byte[] blobContent2,
        ILogger logger)
    {
        logger.LogInformation("Blob sample-container/sample-blob-1 has been updated with content: {content}", Encoding.UTF8.GetString(blobContent1));
        logger.LogInformation("Blob sample-container/sample-blob-2 has content: {content}", Encoding.UTF8.GetString(blobContent2));
    }
}

BLOB へのバイト配列の書き込み

public static class BlobFunction_ByteArray_Write
{
    [FunctionName("BlobFunction")]
    public static void Run(
        [BlobTrigger("sample-container/sample-blob-1")] byte[] blobContent1,
        [Blob("sample-container/sample-blob-2")] out byte[] blobContent2,
        ILogger logger)
    {
        logger.LogInformation("Blob sample-container/sample-blob-1 has been updated with content: {content}", Encoding.UTF8.GetString(blobContent1));
        blobContent2 = blobContent1;
        logger.LogInformation("Blob sample-container/sample-blob-1 has been copied to sample-container/sample-blob-2");
    }
}

TextReader と TextWriter へのバインド

public static class BlobFunction_TextReader_TextWriter
{
    [FunctionName("BlobFunction")]
    public static async Task Run(
        [BlobTrigger("sample-container/sample-blob-1")] TextReader blobContentReader1,
        [Blob("sample-container/sample-blob-2")] TextWriter blobContentWriter2,
        ILogger logger)
    {
        while (blobContentReader1.Peek() >= 0)
        {
            await blobContentWriter2.WriteLineAsync(await blobContentReader1.ReadLineAsync());
        }
        logger.LogInformation("Blob sample-container/sample-blob-1 has been copied to sample-container/sample-blob-2");
    }
}

Azure Storage Blob SDK の種類へのバインド

public static class BlobFunction_BlobClient
{
    [FunctionName("BlobFunction")]
    public static async Task Run(
        [BlobTrigger("sample-container/sample-blob-1")] BlobClient blobClient1,
        [Blob("sample-container/sample-blob-2")] BlobClient blobClient2,
        ILogger logger)
    {
        BlobProperties blobProperties1 = await blobClient1.GetPropertiesAsync();
        logger.LogInformation("Blob sample-container/sample-blob-1 has been updated on: {datetime}", blobProperties1.LastModified);
        BlobProperties blobProperties2 = await blobClient2.GetPropertiesAsync();
        logger.LogInformation("Blob sample-container/sample-blob-2 has been updated on: {datetime}", blobProperties2.LastModified);
    }
}

BLOB コンテナーへのアクセス

public static class BlobFunction_AccessContainer
{
    [FunctionName("BlobFunction")]
    public static async Task Run(
        [BlobTrigger("sample-container/sample-blob")] Stream blobStream,
        [Blob("sample-container")] BlobContainerClient blobContainerClient,
        ILogger logger)
    {
        logger.LogInformation("Blobs within container:");
        await foreach (BlobItem blobItem in blobContainerClient.GetBlobsAsync())
        {
            logger.LogInformation(blobItem.Name);
        }
    }
}

コンテナー内の BLOB の列挙

public static class BlobFunction_EnumerateBlobs_Stream
{
    [FunctionName("BlobFunction")]
    public static async Task Run(
        [BlobTrigger("sample-container/sample-blob")] Stream blobStream,
        [Blob("sample-container")] IEnumerable<Stream> blobs,
        ILogger logger)
    {
        logger.LogInformation("Blobs contents within container:");
        foreach (Stream content in blobs)
        {
            using var blobStreamReader = new StreamReader(content);
            logger.LogInformation(await blobStreamReader.ReadToEndAsync());
        }
    }
}
public static class BlobFunction_EnumerateBlobs_BlobClient
{
    [FunctionName("BlobFunction")]
    public static void Run(
        [BlobTrigger("sample-container/sample-blob")] Stream blobStream,
        [Blob("sample-container")] IEnumerable<BlobClient> blobs,
        ILogger logger)
    {
        logger.LogInformation("Blobs within container:");
        foreach (BlobClient blob in blobs)
        {
            logger.LogInformation(blob.Name);
        }
    }
}

拡張機能を構成する

サンプル関数アプリを参照してください。

トラブルシューティング

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

次のステップ

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

共同作成

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

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

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

インプレッション数