与 AWS SDK或GetRepositoryPolicy一起使用 CLI - AWS SDK代码示例

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

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

与 AWS SDK或GetRepositoryPolicy一起使用 CLI

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

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

CLI
AWS CLI

检索存储库的存储库策略

以下get-repository-policy示例显示了有关存储库存储库策略的cluster-autoscaler详细信息。

aws ecr get-repository-policy \ --repository-name cluster-autoscaler

输出:

{ "registryId": "012345678910", "repositoryName": "cluster-autoscaler", "policyText": "{\n \"Version\" : \"2008-10-17\",\n \"Statement\" : [ {\n \"Sid\" : \"allow public pull\",\n \"Effect\" : \"Allow\",\n \"Principal\" : \"*\",\n \"Action\" : [ \"ecr:BatchCheckLayerAvailability\", \"ecr:BatchGetImage\", \"ecr:GetDownloadUrlForLayer\" ]\n } ]\n}" }
Java
SDK适用于 Java 2.x
注意

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

/** * Gets the repository policy for the specified repository. * * @param repoName the name of the repository. * @throws EcrException if an AWS error occurs while getting the repository policy. */ public String getRepoPolicy(String repoName) { if (repoName == null || repoName.isEmpty()) { throw new IllegalArgumentException("Repository name cannot be null or empty"); } GetRepositoryPolicyRequest getRepositoryPolicyRequest = GetRepositoryPolicyRequest.builder() .repositoryName(repoName) .build(); CompletableFuture<GetRepositoryPolicyResponse> response = getAsyncClient().getRepositoryPolicy(getRepositoryPolicyRequest); response.whenComplete((resp, ex) -> { if (resp != null) { System.out.println("Repository policy retrieved successfully."); } else { if (ex.getCause() instanceof EcrException) { throw (EcrException) ex.getCause(); } else { String errorMessage = "Unexpected error occurred: " + ex.getMessage(); throw new RuntimeException(errorMessage, ex); } } }); GetRepositoryPolicyResponse result = response.join(); return result != null ? result.policyText() : null; }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 GetRepositoryPolicy” 中的。

Kotlin
SDK对于 Kotlin 来说
注意

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

/** * Gets the repository policy for the specified repository. * * @param repoName the name of the repository. */ suspend fun getRepoPolicy(repoName: String?): String? { require(!(repoName == null || repoName.isEmpty())) { "Repository name cannot be null or empty" } // Create the request val getRepositoryPolicyRequest = GetRepositoryPolicyRequest { repositoryName = repoName } EcrClient { region = "us-east-1" }.use { ecrClient -> val response = ecrClient.getRepositoryPolicy(getRepositoryPolicyRequest) val responseText = response.policyText return responseText } }