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管理ライブラリを使用して実行時にサービス エンドポイントに対してクエリを実行することもできます。

Important

クライアント アプリケーションでサービス エンドポイントを参照する場合は、キャッシュされた 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。ID: 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。ストレージ/登録/アクション。 このアクセス許可は、 共同作成者 ロールと 所有者 ロールに含まれています。

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

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

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