DescribeRepositories 搭配 a AWS SDK 或 CLI 使用 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

DescribeRepositories 搭配 a AWS SDK 或 CLI 使用

下列程式碼範例示範如何使用 DescribeRepositories

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

CLI
AWS CLI

描述登錄檔中的儲存庫

此範例說明 帳戶預設登錄檔中的儲存庫。

命令:

aws ecr describe-repositories

輸出:

{ "repositories": [ { "registryId": "012345678910", "repositoryName": "ubuntu", "repositoryArn": "arn:aws:ecr:us-west-2:012345678910:repository/ubuntu" }, { "registryId": "012345678910", "repositoryName": "test", "repositoryArn": "arn:aws:ecr:us-west-2:012345678910:repository/test" } ] }
Java
Java 2.x 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Retrieves the repository URI for the specified repository name. * * @param repoName the name of the repository to retrieve the URI for. * @return the repository URI for the specified repository name. * @throws EcrException if there is an error retrieving the repository information. * @throws CompletionException if the asynchronous operation completes exceptionally. */ public void getRepositoryURI(String repoName) { DescribeRepositoriesRequest request = DescribeRepositoriesRequest.builder() .repositoryNames(repoName) .build(); CompletableFuture<DescribeRepositoriesResponse> response = getAsyncClient().describeRepositories(request); response.whenComplete((describeRepositoriesResponse, ex) -> { if (ex != null) { Throwable cause = ex.getCause(); if (cause instanceof InterruptedException) { Thread.currentThread().interrupt(); String errorMessage = "Thread interrupted while waiting for asynchronous operation: " + cause.getMessage(); throw new RuntimeException(errorMessage, cause); } else if (cause instanceof EcrException) { throw (EcrException) cause; } else { String errorMessage = "Unexpected error: " + cause.getMessage(); throw new RuntimeException(errorMessage, cause); } } else { if (describeRepositoriesResponse != null) { if (!describeRepositoriesResponse.repositories().isEmpty()) { String repositoryUri = describeRepositoriesResponse.repositories().get(0).repositoryUri(); System.out.println("Repository URI found: " + repositoryUri); } else { System.out.println("No repositories found for the given name."); } } else { System.err.println("No response received from describeRepositories."); } } }); response.join(); }
Kotlin
Kotlin 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

/** * Retrieves the repository URI for the specified repository name. * * @param repoName the name of the repository to retrieve the URI for. * @return the repository URI for the specified repository name. */ suspend fun getRepositoryURI(repoName: String?): String? { require(!(repoName == null || repoName.isEmpty())) { "Repository name cannot be null or empty" } val request = DescribeRepositoriesRequest { repositoryNames = listOf(repoName) } EcrClient { region = "us-east-1" }.use { ecrClient -> val describeRepositoriesResponse = ecrClient.describeRepositories(request) if (!describeRepositoriesResponse.repositories?.isEmpty()!!) { return describeRepositoriesResponse?.repositories?.get(0)?.repositoryUri } else { println("No repositories found for the given name.") return "" } } }
Rust
Rust 的 SDK
注意

還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

async fn show_repos(client: &aws_sdk_ecr::Client) -> Result<(), aws_sdk_ecr::Error> { let rsp = client.describe_repositories().send().await?; let repos = rsp.repositories(); println!("Found {} repositories:", repos.len()); for repo in repos { println!(" ARN: {}", repo.repository_arn().unwrap()); println!(" Name: {}", repo.repository_name().unwrap()); } Ok(()) }