Azure Storage 管理ライブラリを使用して Blob Storage エンドポイントのクエリを実行する方法

Blob Storage エンドポイントは、ストレージ アカウント内のすべてのオブジェクトのベース アドレスを形成します。 ストレージ アカウントを作成するときは、使用するエンドポイントの種類を指定します。 Blob Storage では、2 種類のエンドポイントがサポートされています。

  • 標準エンドポイントには、一意のストレージ アカウント名と固定ドメイン名が含まれます。 標準エンドポイントの形式は https://<storage-account>.blob.core.windows.net です。
  • Azure DNS ゾーン エンドポイント (プレビュー) では、Azure DNS ゾーンが動的に選択され、作成時にストレージ アカウントに割り当てられます。 Azure DNS ゾーン エンドポイントの形式は https://<storage-account>.z[00-99].blob.storage.azure.net です。

Blob Storage データ リソースに接続するサービス クライアント オブジェクトをアプリケーションで作成する場合は、エンドポイントを参照する URI をサービス クライアント コンストラクターに渡します。 URI 文字列は手動で作成することも、Azure Storage 管理ライブラリを使用して実行時にサービス エンドポイントに対してクエリを実行することもできます。

重要

クライアント アプリケーションでサービス エンドポイントを参照する場合は、キャッシュされた IP アドレスに依存しないようにすることをお勧めします。 ストレージ アカウントの IP アドレスは変更される可能性があるため、キャッシュされた IP アドレスに依存すると、予期しない動作が発生する可能性があります。

ストレージ アカウント エンドポイントに関連付けられている CNAME は、予告なしに変更される可能性があります。 アプリケーションは、CNAME レコードの数や、それらの CNAME レコードに関連付けられているサブドメインに依存しないようにする必要があります。

さらに、DNS レコードの有効期限 (TTL) を守り、オーバーライドしないようにすることをお勧めします。 DNS TTL をオーバーライドすると、予期しない動作が発生する可能性があります。

詳細については、「CNAME レコード、サブドメイン、および IP アドレス」を参照してください。

Azure Storage 管理ライブラリは、Azure Storage リソース プロバイダーへのプログラムによるアクセスを提供します。 リソース プロバイダーは、Azure Resource Manager の Azure Storage 実装です。 管理ライブラリを使用すると、開発者はストレージ アカウントとアカウント構成を管理したり、ライフサイクル管理ポリシー、オブジェクト レプリケーション ポリシー、不変ポリシーを構成したりできます。

この記事では、Azure Storage 管理ライブラリを使用して Blob Storage エンドポイントのクエリを実行する方法について説明します。 その後、そのエンドポイントを使用して、Blob Storage データ リソースに接続する BlobServiceClient オブジェクトを作成します。

プロジェクトの設定

この記事のコード例を使用するには、次の手順に従ってプロジェクトを設定します。

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

この例で使用されているライブラリを操作するためのパッケージをインストールします。

dotnet add package を使用して次のパッケージをインストールしてください。

dotnet add package Azure.Identity
dotnet add package Azure.ResourceManager.Storage
dotnet add package Azure.Storage.Blobs

アプリ コードを設定する

必要な using または import ディレクティブをコードに追加します。 コード例ではファイル間で機能が分割される場合がありますが、このセクションではすべてのディレクティブが一緒に一覧表示されます。

次の using ディレクティブを追加します。

using Azure.Core;
using Azure.Identity;
using Azure.Storage.Blobs;
using Azure.ResourceManager;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Storage;

クライアント ライブラリ情報:

  • Azure.Identity: Azure SDK 全体で Microsoft Entra トークン認証のサポートを提供し、Azure サービスへのパスワードレス接続に必要です。
  • Azure.ResourceManager.Storage: リソース グループやストレージ アカウントなど、Azure Storage リソースの管理をサポートします。
  • Azure.Storage.Blobs: Blob Storage データ リソースの操作に使用できるプライマリ クラスが含まれています。

ストレージ リソースプロバイダーをサブスクリプションに登録する

リソース プロバイダーを使用するには、Azure サブスクリプションに登録しておく必要があります。 この手順は、サブスクリプションごとに 1 回だけ実行する必要があり、リソース プロバイダー Microsoft.Storage が現在サブスクリプションに登録されていない場合にのみ適用されます。

ストレージ リソース プロバイダーを登録するか、Azure portalAzure CLI、または Azure PowerShell を使用して登録状態をチェックできます。

次の例に示すように、Azure 管理ライブラリを使用して登録状態をチェックし、ストレージ リソース プロバイダーを登録することもできます。

public static async Task RegisterSRPInSubscription(SubscriptionResource subscription)
{
    ResourceProviderResource resourceProvider = 
        await subscription.GetResourceProviderAsync("Microsoft.Storage");

    // Check the registration state of the resource provider and register, if needed
    if (resourceProvider.Data.RegistrationState == "NotRegistered")
        resourceProvider.Register();
}

Note

登録操作を実行するには、次の Azure RBAC アクション Microsoft.Storage/register/action に対するアクセス許可が必要です。 このアクセス許可は、共同作成者所有者ロールに含まれます。

Blob Storage エンドポイントのクエリ

特定のストレージ アカウントの Blob Storage エンドポイントを取得するには、Get Properties 操作を呼び出してストレージ アカウントのプロパティを取得する必要があります。 次のコード サンプルでは、データ アクセス ライブラリと管理ライブラリの両方を使用して、指定されたストレージ アカウントの Blob Storage エンドポイントを取得します。

指定したストレージ アカウントのプロパティを取得するには、StorageAccountCollection オブジェクトから次のメソッドを使用します。

このメソッドは、ストレージ アカウントを表す StorageAccountResource オブジェクトを返します。

public static async Task<Uri> GetBlobServiceEndpoint(
    string storageAccountName,
    TokenCredential credential)
{
    // TODO: replace with your subscription ID and resource group name
    // You can locate your subscription ID on the Subscriptions blade
    // of the Azure portal (https://portal.azure.com)
    const string subscriptionId = "<subscription-id>";
    const string rgName = "<resource-group-name>";

    ArmClient armClient = new(credential);

    // Create a resource identifier, then get the subscription resource
    ResourceIdentifier resourceIdentifier = new($"/subscriptions/{subscriptionId}");
    SubscriptionResource subscription = armClient.GetSubscriptionResource(resourceIdentifier);

    // Get a resource group
    ResourceGroupResource resourceGroup = await subscription.GetResourceGroupAsync(rgName);

    // Get a collection of storage account resources
    StorageAccountCollection accountCollection = resourceGroup.GetStorageAccounts();

    // Get the properties for the specified storage account
    StorageAccountResource storageAccount = await accountCollection.GetAsync(storageAccountName);

    // Return the primary endpoint for the blob service
    return storageAccount.Data.PrimaryEndpoints.BlobUri;
}

エンドポイントを使用してクライアント オブジェクトを作成する

ストレージ アカウントの Blob Storage エンドポイントを取得したら、クライアント オブジェクトをインスタンス化してデータ リソースを操作できます。 次のコード サンプルでは、前の例で取得したエンドポイントを使用して BlobServiceClient オブジェクトを作成します。

// Create an instance of DefaultAzureCredential for authorization
TokenCredential credential = new DefaultAzureCredential();

// TODO: replace with your storage account name
string storageAccountName = "<storage-account-name>";

// Call out to our function that retrieves the blob service endpoint for the given storage account
Uri blobURI = await AccountProperties.GetBlobServiceEndpoint(storageAccountName, credential);
Console.WriteLine($"URI: {blobURI}");

// Now that we know the endpoint, create the client object
BlobServiceClient blobServiceClient = new(blobURI, credential);

// Do something with the storage account or its resources ...

次のステップ

完全なコード サンプルを表示する (GitHub):

クライアント オブジェクトの作成の詳細については、「データ リソースを操作するクライアント オブジェクトを作成および管理する」を参照してください。