JavaScript を使用して BLOB コンテナーを削除して復元する

この記事では、JavaScript 用の Azure Storage クライアント ライブラリを使ってコンテナーを削除する方法について説明します。 コンテナーの論理的な削除を有効にしている場合は、削除されたコンテナーを復元できます。

前提条件

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

コンテナーを削除する

JavaScript でコンテナーを削除するには、BlobServiceClient または ContainerClient を作成し、次のいずれかのメソッドを使用します。

コンテナーを削除した後、少なくとも 30 秒間は同じ名前のコンテナーを作成することはできません。 同じ名前のコンテナーを作成しようとすると、HTTP エラー コード 409 (Conflict) が返されて処理が失敗します。 コンテナーまたはそれに含まれる BLOB に対して他の操作を実行しようとすると、HTTP エラー コード 404 (Not Found) が返されて処理が失敗します。

BlobServiceClient を使用してコンテナーを削除する

次の例では、指定したコンテナーを削除します。 BlobServiceClient を使用してコンテナーを削除します。

// delete container immediately on blobServiceClient
async function deleteContainerImmediately(blobServiceClient, containerName) {
  const response = await blobServiceClient.deleteContainer(containerName);

  if (!response.errorCode) {
    console.log(`deleted ${containerItem.name} container`);
  }
}

ContainerClient を使用してコンテナーを削除する

次の例は、ContainerClient を使用して、その名前の先頭に指定したプレフィックスがあるすべてのコンテナーを削除する方法を示しています。

async function deleteContainersWithPrefix(blobServiceClient, blobNamePrefix){

  const containerOptions = {
    includeDeleted: false,
    includeMetadata: false,
    includeSystem: true,
    prefix: blobNamePrefix
  }

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

    const containerClient = blobServiceClient.getContainerClient(containerItem.name);

    const response = await containerClient.delete();

    if(!response.errorCode){
      console.log(`deleted ${containerItem.name} container`);
    }
  }
}

削除されたコンテナーを復元する

ストレージ アカウントに対してコンテナーの論理的な削除が有効になっている場合、コンテナーとその内容は、削除された後も、指定した保持期間中は復旧することができます。 論理的に削除されたコンテナーは、 BlobServiceClient オブジェクトを使用して復元できます。

次の例では、削除されたコンテナーを検索し、その削除されたコンテナーのバージョン ID を取得した後、その ID を undeleteContainer メソッドに渡してコンテナーを復元します。

// Undelete specific container - last version
async function undeleteContainer(blobServiceClient, containerName) {

  // version to undelete
  let containerVersion;

  const containerOptions = {
    includeDeleted: true,
    prefix: containerName
  }

  // container listing returns version (timestamp) in the ContainerItem
  for await (const containerItem of blobServiceClient.listContainers(containerOptions)) {

    // if there are multiple deleted versions of the same container,
    // the versions are in asc time order
    // the last version is the most recent
    if (containerItem.name === containerName) {
      containerVersion = containerItem.version;
    }
  }

  const containerClient = await blobServiceClient.undeleteContainer(
    containerName,
    containerVersion,

    // optional/new container name - if unused, original container name is used
    //newContainerName 
  );

  // undelete was successful
  console.log(`${containerName} is undeleted`);

  // do something with containerClient
  // ...
}

リソース

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

REST API の操作

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

コード サンプル

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

こちらもご覧ください