文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
GetRepositoryPolicy
搭配 a AWS SDK 或 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}" }
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetRepositoryPolicy
。
-
- Java
-
- Java 2.x 的 SDK
-
注意
還有更多 on 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 詳細資訊,請參閱 GetRepositoryPolicy AWS SDK for Java 2.x 參考中的 API。
-
- Kotlin
-
- Kotlin 的 SDK
-
注意
還有更多 on 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 } }
-
如需 API 詳細資訊,請參閱 GetRepositoryPolicy
AWS for Kotlin SDK 參考中的 API。
-
GetAuthorizationToken
ListImages