你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Java 删除和还原 Blob 容器

本文介绍了如何使用适用于 Java 的 Azure 存储客户端库删除容器。 如果已启用容器软删除,则可以还原已删除的容器。

先决条件

设置你的环境

如果没有现有项目,请查看本部分,其中介绍了如何设置项目来使用适用于 Java 的 Azure Blob 存储客户端库。 有关详细信息,请参阅 Azure Blob 存储和 Java 入门

要使用本文中的代码示例,请按照以下步骤设置项目。

注意

本文使用 Maven 生成工具来生成和运行示例代码。 其他生成工具(例如 Gradle)也可与 Azure SDK for Java 一起使用。

安装包

在文本编辑器中打开 pom.xml 文件pom.xml。 通过包括 BOM 文件包括直接依赖项来安装包。

添加 import 语句

添加以下 import 语句:

import com.azure.storage.blob.*;
import com.azure.storage.blob.models.*;

授权

授权机制必须具有删除或还原容器所需的权限。 若要使用 Microsoft Entra ID 进行授权(建议),需有 Azure RBAC 内置角色“存储 Blob 数据参与者”或更高级别的角色。 有关详细信息,请参阅删除容器 (REST API)还原容器 (REST API) 的授权指南。

创建客户端对象

若要将应用连接到 Blob 存储,请创建 BlobServiceClient 的实例。

以下示例使用 BlobServiceClientBuilder 生成一个使用 DefaultAzureCredentialBlobServiceClient 对象,并演示如何创建容器和 Blob 客户端(如果需要):

// Azure SDK client builders accept the credential as a parameter
// TODO: Replace <storage-account-name> with your actual storage account name
BlobServiceClient 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 BlobServiceClient
BlobContainerClient containerClient = blobServiceClient
        .getBlobContainerClient("<container-name>");

// If needed, you can create a BlobClient object from the BlobContainerClient
BlobClient 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 存储客户端库删除容器,请参阅以下资源。

代码示例

REST API 操作

Azure SDK for Java 包含基于 Azure REST API 而生成的库,允许你通过熟悉的 Java 范例与 REST API 操作进行交互。 用于删除或还原容器的客户端库方法使用以下 REST API 操作:

客户端库资源

请参阅

  • 本文是适用于 Java 的 Blob 存储开发人员指南的一部分。 若要了解详细信息,请参阅构建 Java 应用中的完整开发人员指南文章列表。