TypeScript を使って BLOB コンテナーを一覧表示する

Azure Storage アカウント内のコンテナーをコードから一覧表示する際には、Azure Storage からの結果の取得方法を管理するためのオプションをいくつか指定できます。 この記事では、JavaScript 用の Azure Storage クライアント ライブラリを使用してコンテナーを一覧表示する方法について説明します。

前提条件

  • この記事の例では、JavaScript 用の Azure Blob Storage クライアント ライブラリを操作するための設定が済んだプロジェクトが、既にあることを前提としています。 パッケージのインストール、モジュールのインポート、データ リソースを操作するための認可済みクライアント オブジェクトの作成など、プロジェクトのセットアップについては、「Azure Blob Storage と TypeScript の概要」を参照してください。
  • 認可メカニズム には、BLOB コンテナーを一覧表示するためのアクセス許可が必要です。 詳細については、次の REST API 操作の認可ガイダンスを参照してください。

コンテナーの一覧表示のオプションについて

ストレージ アカウント内のコンテナーを一覧表示するには、BlobServiceClient オブジェクトを作成してから、次のメソッドを呼び出します。

省略可能なプレフィックスを使用してコンテナーを一覧表示する

既定では、一覧表示操作では一度に最大 5000 の結果が返されます。

BlobServiceClient.listContainers からは、ContainerItem オブジェクトのリストが返されます。 より完全な ContainerProperties オブジェクトを取得するには、containerItem.name を使用して ContainerClient を作成します。

// return up to 5000 containers
async function listContainers(
  blobServiceClient: BlobServiceClient,
  containerNamePrefix: string
): Promise<void> {
  const options: ServiceListContainersOptions = {
    includeDeleted: false,
    includeMetadata: true,
    includeSystem: true,
    prefix: containerNamePrefix
  };

  for await (const containerItem of blobServiceClient.listContainers(options)) {
    // ContainerItem
    console.log(`For-await list: ${containerItem.name}`);

    // ContainerClient
    const containerClient: ContainerClient =
      blobServiceClient.getContainerClient(containerItem.name);

    // ... do something with container
    // containerClient.listBlobsFlat({    includeMetadata: true,
    // includeSnapshots: false,
    // includeTags: true,
    // includeVersions: false,
    // prefix: ''});
  }
}

ページングを使用してコンテナーを一覧表示する

返される結果セットが小さくなるようにするには、返される結果ページのサイズに 0 以外の値を指定します。

ストレージ アカウントに 5000 を超えるコンテナーが含まれている場合、または一覧表示操作によってストレージ アカウント内のコンテナーのサブセットが返されるようにページ サイズを指定した場合、Azure Storage はコンテナーの一覧と共に "継続トークン" を返します。 継続トークンは、Azure Storage から次の結果セットを取得するために使用できる非透過の値です。

コードでは、後続トークンの値をチェックして、それが空かどうかを確認します。 後続トークンが空の場合、結果セットは完了しています。 後続トークンが空でない場合は、一覧表示メソッドをもう一度呼び出し、後続トークンを渡して次の結果セットを取得する手順を、後続トークン空になるまで繰り返します。

async function listContainersWithPagingMarker(
  blobServiceClient: BlobServiceClient
) {
  // add prefix to filter list
  const containerNamePrefix = '';

  // page size
  const maxPageSize = 2;

  const options: ServiceListContainersOptions = {
    includeDeleted: false,
    includeMetadata: true,
    includeSystem: true,
    prefix: containerNamePrefix
  };

  let i = 1;

  let iterator = blobServiceClient
    .listContainers(options)
    .byPage({ maxPageSize });
  let response = (await iterator.next()).value;

  // Prints 2 container names
  if (response.containerItems) {
    for (const container of response.containerItems) {
      console.log(`IteratorPaged: Container ${i++}: ${container.name}`);
    }
  }

  // Gets next marker
  const marker = response.continuationToken;

  // Passing next marker as continuationToken
  iterator = blobServiceClient
    .listContainers()
    .byPage({ continuationToken: marker, maxPageSize: maxPageSize * 2 });
  response = (await iterator.next()).value;

  // Print next 4 container names
  if (response.containerItems) {
    for (const container of response.containerItems) {
      console.log(`Container ${i++}: ${container.name}`);
    }
  }
}

プレフィックスを使用して結果をフィルター処理するには、listContainers メソッドの options パラメーターを使用します。

プレフィックスを使用して結果をフィルター処理する

コンテナーの一覧をフィルター処理するには、prefix プロパティの文字列を指定します。 プレフィックス文字列には、1 つ以上の文字を含めることができます。 Azure Storage は、名前がそのプレフィックスで始まるコンテナーだけを返します。

async function listContainers(
  blobServiceClient: BlobServiceClient,
  containerNamePrefix: string
) {

  const options: ServiceListContainersOptions = {
    includeDeleted: false,
    includeMetadata: true,
    includeSystem: true,

    // filter by prefix
    prefix: containerNamePrefix
  };

  for await (const containerItem of blobServiceClient.listContainers(options)) {


    // do something with containerItem

  }
}

結果にメタデータを含める

結果と共にコンテナーのメタデータを返すには、BlobContainerTraits 列挙型のメタデータ値を指定します。 Azure Storage では、返される各コンテナーにメタデータが含まれているため、別の操作としてコンテナーのメタデータをフェッチする必要はありません。

async function listContainers(
  blobServiceClient: BlobServiceClient,
  containerNamePrefix: string
) {

  const options: ServiceListContainersOptions = {
    includeDeleted: false,
    includeSystem: true,
    prefix: containerNamePrefix,

    // include metadata
    includeMetadata: true,
  };

  for await (const containerItem of blobServiceClient.listContainers(options)) {

    // do something with containerItem

  }
}

リソース

JavaScript 用 Azure Blob Storage クライアント ライブラリを使用したコンテナーの一覧表示の詳細については、次のリソースを参照してください。

REST API の操作

Azure SDK for JavaScript には Azure REST API に基づいて構築されたライブラリが含まれるため、使い慣れた JavaScript パラダイムを通じて REST API 操作を利用できます。 コンテナーを一覧表示するためのクライアント ライブラリ メソッドでは、次の REST API 操作が使用されます。

コード サンプル

クライアント ライブラリのリソース

こちらもご覧ください