使用 TypeScript 删除和还原 blob 容器

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

先决条件

  • 本文中的示例假设你已经设置了一个项目来使用适用于 JavaScript 的 Azure Blob 存储客户端库。 若要了解如何设置项目(包括安装包、导入模块,以及创建授权客户端对象来使用数据资源),请参阅开始使用 Azure Blob 存储和 TypeScript
  • 授权机制必须具有删除 blob 容器或还原软删除容器的权限。 若要了解详细信息,请参阅以下 REST API 操作的授权指南:

删除容器

若要在 TypeScript 中删除容器,请创建 BlobServiceClient 或 ContainerClient,然后使用以下方法之一:

删除容器后,至少在 30 秒内无法使用相同的名称创建容器。 尝试使用相同的名称创建容器将会失败,并出现 HTTP 错误代码 409(冲突)。 针对容器或其包含的 Blob 执行任何其他操作将会失败,并出现 HTTP 错误代码 404(未找到)。

使用 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 对象还原软删除的容器:

以下示例查找已删除的容器,获取该已删除容器的版本 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 存储客户端库来删除容器,请参阅以下资源。

REST API 操作

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

代码示例

客户端库资源

另请参阅