このブラウザーはサポートされなくなりました。
Microsoft Edge にアップグレードすると、最新の機能、セキュリティ更新プログラム、およびテクニカル サポートを利用できます。
この記事では、Java 用の Azure Storage クライアント ライブラリを使ってコンテナーを削除する方法について説明します。コンテナーの論理的な削除を有効にしている場合は、削除されたコンテナーを復元できます。
既存のプロジェクトがない場合、Java 用 Azure Blob Storage クライアント ライブラリを操作するためにプロジェクトをセットアップする方法について、このセクションで説明します。 詳細については、「Azure Blob Storage と Java での作業開始」を参照してください。
この記事のコード例を使用するには、次の手順に従ってプロジェクトを設定します。
Note
この記事では Maven ビルド ツールを使用して、コード例をビルドして実行します。 Gradle などの他のビルド ツールも、Azure SDK for Java で動作します。
テキスト エディターでpom.xml
ファイルを開きます。BOM ファイルを含めるか、直接依存関係を含めることで、パッケージをインストールします。
次のimport
ステートメントを追加します。
import com.azure.storage.blob.*;import com.azure.storage.blob.models.*;
認可メカニズムには、コンテナーを削除または復元するために必要なアクセス許可が必要です。 Microsoft Entra ID を使用した認可 (推奨) には、Azure RBAC 組み込みロールのストレージ BLOB データ共同作成者以上が必要です。 詳細については、「コンテナーを削除する (REST API)」と「コンテナーを復元する (REST API)」に関する認可ガイダンスを参照してください。
アプリを Blob Storage に接続するには、BlobServiceClientのインスタンスを作成します。
次の例では、BlobServiceClientBuilder を使用し、DefaultAzureCredential
を使用してBlobServiceClient
オブジェクトをビルドし、必要な場合にコンテナーおよび BLOB クライアントを作成する方法を示します。
// Azure SDK client builders accept the credential as a parameter// TODO: Replace <storage-account-name> with your actual storage account nameBlobServiceClient blobServiceClient = new BlobServiceClientBuilder() .endpoint("https://<storage-account-name>.blob.core.windows.net/") .credential(new DefaultAzureCredentialBuilder().build()) .buildClient();// If needed, you can create a BlobContainerClient object from the BlobServiceClientBlobContainerClient containerClient = blobServiceClient .getBlobContainerClient("<container-name>");// If needed, you can create a BlobClient object from the BlobContainerClientBlobClient blobClient = containerClient .getBlobClient("<blob-name>");
クライアント オブジェクトの作成と管理の詳細については、「データ リソースを操作するクライアント オブジェクトを作成および管理する」を参照してください。
Java でコンテナーを削除するには、BlobServiceClient
クラスの次のいずれかのメソッドを使用します。
次に示すBlobContainerClient
クラスのメソッドのいずれかを使用してコンテナーを削除することもできます。
コンテナーを削除した後、"少なくとも" 30 秒間は同じ名前のコンテナーを作成できません。 同じ名前のコンテナーを作成しようとすると、HTTP エラー コード409 (Conflict)
が返されて処理が失敗します。 コンテナーまたはそれに含まれる BLOB に対して他の操作を実行しようとすると、HTTP エラー コード404 (Not Found)
が返されて処理が失敗します。
次の例では、BlobServiceClient
オブジェクトを使用して、指定したコンテナーを削除しています。
public void deleteContainer(BlobServiceClient blobServiceClient, String containerName) { // Delete the container using the service client blobServiceClient.deleteBlobContainer(containerName);}
次の例は、指定したプレフィックスで始まるすべてのコンテナーを削除する方法を示しています。
public void deleteContainersWithPrefix(BlobServiceClient blobServiceClient) { ListBlobContainersOptions options = new ListBlobContainersOptions() .setPrefix("container-"); // Delete the container with the specified prefix using the service client for (BlobContainerItem containerItem : blobServiceClient.listBlobContainers(options, null)) { BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerItem.getName()); containerClient.delete(); }}
ストレージ アカウントに対してコンテナーの論理的な削除が有効になっているとき、指定した保持期間中はコンテナーとその内容を復旧できます。 コンテナーの論理的な削除の詳細については、「コンテナーの論理的な削除を有効にして管理する」をご覧ください。BlobServiceClient
クラスの次のメソッドを呼び出すことで、論理的に削除されたコンテナーを復元できます。
次の例では、削除されたコンテナーを検索し、その削除されたコンテナーのバージョンを取得し、次にそのバージョンをundeleteBlobContainer
メソッドに渡してコンテナーを復元します。
public void restoreContainer(BlobServiceClient blobServiceClient) { ListBlobContainersOptions options = new ListBlobContainersOptions(); options.getDetails().setRetrieveDeleted(true); // Delete the container with the specified prefix using the service client for (BlobContainerItem deletedContainerItem : blobServiceClient.listBlobContainers(options, null)) { BlobContainerClient containerClient = blobServiceClient .undeleteBlobContainer(deletedContainerItem.getName(), deletedContainerItem.getVersion()); }}
Java 用 Azure Blob Storage クライアント ライブラリを使用したコンテナーの削除の詳細については、次のリソースを参照してください。
Azure SDK for Java には Azure REST API に基づき構築されたライブラリが含まれるため、使い慣れた Java パラダイムを通じて REST API 操作を実施できます。 コンテナーを削除または復元するためのクライアント ライブラリ メソッドでは、次の REST API 操作が使用されます。
このページはお役に立ちましたか?
このページはお役に立ちましたか?