DeleteRepository与 AWS SDK 或 CLI 配合使用 - AWS SDK 代码示例

文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

DeleteRepository与 AWS SDK 或 CLI 配合使用

以下代码示例演示如何使用 DeleteRepository

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

CLI
AWS CLI

删除存储库

以下delete-repository示例命令强制删除帐户默认注册表中的指定存储库。如果存储库包含图像,则必须使用该--force标志。

aws ecr delete-repository \ --repository-name ubuntu \ --force

输出:

{ "repository": { "registryId": "123456789012", "repositoryName": "ubuntu", "repositoryArn": "arn:aws:ecr:us-west-2:123456789012:repository/ubuntu" } }

有关更多信息,请参阅 Amazon ECR 用户指南中的删除存储库

  • 有关 API 的详细信息,请参阅AWS CLI 命令参考DeleteRepository中的。

Java
适用于 Java 的 SDK 2.x
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整实例,了解如何进行设置和运行。

/** * Deletes an ECR (Elastic Container Registry) repository. * * @param repoName the name of the repository to delete. * @throws IllegalArgumentException if the repository name is null or empty. * @throws EcrException if there is an error deleting the repository. * @throws RuntimeException if an unexpected error occurs during the deletion process. */ public void deleteECRRepository(String repoName) { if (repoName == null || repoName.isEmpty()) { throw new IllegalArgumentException("Repository name cannot be null or empty"); } DeleteRepositoryRequest repositoryRequest = DeleteRepositoryRequest.builder() .force(true) .repositoryName(repoName) .build(); CompletableFuture<DeleteRepositoryResponse> response = getAsyncClient().deleteRepository(repositoryRequest); response.whenComplete((deleteRepositoryResponse, ex) -> { if (deleteRepositoryResponse != null) { System.out.println("You have successfully deleted the " + repoName + " repository"); } else { Throwable cause = ex.getCause(); if (cause instanceof EcrException) { throw (EcrException) cause; } else { throw new RuntimeException("Unexpected error: " + cause.getMessage(), cause); } } }); // Wait for the CompletableFuture to complete response.join(); }
  • 有关 API 的详细信息,请参阅 AWS SDK for Java 2.x API 参考DeleteRepository中的。

Kotlin
适用于 Kotlin 的 SDK
注意

还有更多相关信息 GitHub。在 AWS 代码示例存储库中查找完整实例,了解如何进行设置和运行。

/** * Deletes an ECR (Elastic Container Registry) repository. * * @param repoName the name of the repository to delete. */ suspend fun deleteECRRepository(repoName: String) { if (repoName.isNullOrEmpty()) { throw IllegalArgumentException("Repository name cannot be null or empty") } val repositoryRequest = DeleteRepositoryRequest { force = true repositoryName = repoName } EcrClient { region = "us-east-1" }.use { ecrClient -> ecrClient.deleteRepository(repositoryRequest) println("You have successfully deleted the $repoName repository") } }
  • 有关 API 的详细信息,请参阅适用DeleteRepository于 K otlin 的AWS SDK API 参考