TypeScript を使った BLOB コンテナーの削除と復元
この記事では、JavaScript 用の Azure Storage クライアント ライブラリを使ってコンテナーを削除する方法について説明します。 コンテナーの論理的な削除を有効にしている場合は、削除されたコンテナーを復元できます。
前提条件
- この記事の例では、JavaScript 用の Azure Blob Storage クライアント ライブラリを操作するための設定が済んだプロジェクトが、既にあることを前提としています。 パッケージのインストール、モジュールのインポート、データ リソースを操作するための認可済みクライアント オブジェクトの作成など、プロジェクトのセットアップについては、「Azure Blob Storage と TypeScript の概要」を参照してください。
- 認可メカニズムには、BLOB コンテナーを削除するためのアクセス許可、または論理的に削除されたコンテナーを復元するためのアクセス許可が必要です。 詳細については、次の REST API 操作の認可ガイダンスを参照してください。
コンテナーを削除する
TypeScript でコンテナーを削除するには、BlobServiceClient または ContainerClient を作成し、次のいずれかのメソッドを使います。
- BlobServiceClient.deleteContainer
- ContainerClient.delete
- ContainerClient.deleteIfExists
コンテナーを削除した後、少なくとも 30 秒間は同じ名前のコンテナーを作成することはできません。 同じ名前のコンテナーを作成しようとすると、HTTP エラー コード 409 (Conflict) が返されて処理が失敗します。 コンテナーまたはそれに含まれる BLOB に対して他の操作を実行しようとすると、HTTP エラー コード 404 (Not Found) が返されて処理が失敗します。
BlobServiceClient を使用してコンテナーを削除する
次の例では、指定したコンテナーを削除します。 BlobServiceClient を使用してコンテナーを削除します。
// delete container immediately on blobServiceClient
async function deleteContainerImmediately(
blobServiceClient: BlobServiceClient,
containerName: string
): Promise<ContainerDeleteResponse> {
return await blobServiceClient.deleteContainer(containerName);
}
ContainerClient を使用してコンテナーを削除する
次の例は、ContainerClient を使用して、その名前の先頭に指定したプレフィックスがあるすべてのコンテナーを削除する方法を示しています。
async function deleteContainersWithPrefix(
blobServiceClient: BlobServiceClient,
prefix: string
): Promise<void> {
const containerOptions: ServiceListContainersOptions = {
// only delete containers not deleted
includeDeleted: false,
includeMetadata: false,
includeSystem: true,
prefix
};
for await (const containerItem of blobServiceClient.listContainers(
containerOptions
)) {
try {
const containerClient: ContainerClient =
blobServiceClient.getContainerClient(containerItem.name);
const containerDeleteMethodOptions: ContainerDeleteMethodOptions = {};
await containerClient.delete(containerDeleteMethodOptions);
console.log(`deleted ${containerItem.name} container - success`);
} catch (err: unknown) {
if (err instanceof Error) {
console.log(
`deleted ${containerItem.name} container - failed - ${err.message}`
);
}
}
}
}
削除されたコンテナーを復元する
ストレージ アカウントに対してコンテナーの論理的な削除が有効になっている場合、コンテナーとその内容は、削除された後も、指定した保持期間中は復旧することができます。 論理的に削除されたコンテナーは、 BlobServiceClient オブジェクトを使用して復元できます。
- BlobServiceClient.undeleteContainer
次の例では、削除されたコンテナーを検索し、その削除されたコンテナーのバージョン ID を取得した後、その ID を undeleteContainer メソッドに渡してコンテナーを復元します。
// Undelete specific container - last version
async function undeleteContainer(
blobServiceClient: BlobServiceClient,
containerName: string
): Promise<void> {
// version to undelete
let containerVersion: string | undefined;
const containerOptions: ServiceListContainersOptions = {
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 as string;
}
}
if (containerVersion !== undefined) {
const serviceUndeleteContainerOptions: ServiceUndeleteContainerOptions = {};
const {
containerClient,
containerUndeleteResponse
}: {
containerClient: ContainerClient;
containerUndeleteResponse: ContainerUndeleteResponse;
} = await blobServiceClient.undeleteContainer(
containerName,
containerVersion,
// optional/new container name - if unused, original container name is used
//newContainerName
serviceUndeleteContainerOptions
);
// Check delete
if (containerUndeleteResponse.errorCode)
throw Error(containerUndeleteResponse.errorCode);
// undelete was successful
console.log(`${containerName} is undeleted`);
// do something with containerClient
// ...
// containerClient.listBlobsFlat({ includeMetadata: true,
// includeSnapshots: false,
// includeTags: true,
// includeVersions: false,
// prefix: ''});
}
}
リソース
JavaScript 用 Azure Blob Storage クライアント ライブラリを使用したコンテナーの削除の詳細については、次のリソースを参照してください。
REST API の操作
Azure SDK for JavaScript には Azure REST API に基づいて構築されたライブラリが含まれるため、使い慣れた JavaScript パラダイムを通じて REST API 操作を利用できます。 コンテナーを削除または復元するためのクライアント ライブラリ メソッドでは、次の REST API 操作が使用されます。
- Delete Container (REST API)
- コンテナーを復元する (REST API)