選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

使用 AWS SDKs Amazon ECR 程式碼範例

焦點模式
使用 AWS SDKs Amazon ECR 程式碼範例 - Amazon ECR

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

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

下列程式碼範例示範如何搭配 AWS 軟體開發套件 (SDK) 使用 Amazon ECR。

基本概念是程式碼範例,這些範例說明如何在服務內執行基本操作。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。

如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱 搭配 AWS SDK 使用 Amazon ECR。此主題也包含入門相關資訊和舊版 SDK 的詳細資訊。

開始使用

下列程式碼範例示範如何使用 Amazon ECR。

Java
SDK for Java 2.x
注意

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

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecr.EcrClient; import software.amazon.awssdk.services.ecr.model.EcrException; import software.amazon.awssdk.services.ecr.model.ListImagesRequest; import software.amazon.awssdk.services.ecr.paginators.ListImagesIterable; public class HelloECR { public static void main(String[] args) { final String usage = """ Usage: <repositoryName> Where: repositoryName - The name of the Amazon ECR repository. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String repoName = args[0]; EcrClient ecrClient = EcrClient.builder() .region(Region.US_EAST_1) .build(); listImageTags(ecrClient, repoName); } public static void listImageTags(EcrClient ecrClient, String repoName){ ListImagesRequest listImagesPaginator = ListImagesRequest.builder() .repositoryName(repoName) .build(); ListImagesIterable imagesIterable = ecrClient.listImagesPaginator(listImagesPaginator); imagesIterable.stream() .flatMap(r -> r.imageIds().stream()) .forEach(image -> System.out.println("The docker image tag is: " +image.imageTag())); } }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 listImages

Kotlin
SDK for Kotlin
注意

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

import aws.sdk.kotlin.services.ecr.EcrClient import aws.sdk.kotlin.services.ecr.model.ListImagesRequest import kotlin.system.exitProcess suspend fun main(args: Array<String>) { val usage = """ Usage: <repositoryName> Where: repositoryName - The name of the Amazon ECR repository. """.trimIndent() if (args.size != 1) { println(usage) exitProcess(1) } val repoName = args[0] listImageTags(repoName) } suspend fun listImageTags(repoName: String?) { val listImages = ListImagesRequest { repositoryName = repoName } EcrClient { region = "us-east-1" }.use { ecrClient -> val imageResponse = ecrClient.listImages(listImages) imageResponse.imageIds?.forEach { imageId -> println("Image tag: ${imageId.imageTag}") } } }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Kotlin API 參考中的 listImages

Python
SDK for Python (Boto3)
注意

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

import boto3 import argparse from boto3 import client def hello_ecr(ecr_client: client, repository_name: str) -> None: """ Use the AWS SDK for Python (Boto3) to create an Amazon Elastic Container Registry (Amazon ECR) client and list the images in a repository. This example uses the default settings specified in your shared credentials and config files. :param ecr_client: A Boto3 Amazon ECR Client object. This object wraps the low-level Amazon ECR service API. :param repository_name: The name of an Amazon ECR repository in your account. """ print( f"Hello, Amazon ECR! Let's list some images in the repository '{repository_name}':\n" ) paginator = ecr_client.get_paginator("list_images") page_iterator = paginator.paginate( repositoryName=repository_name, PaginationConfig={"MaxItems": 10} ) image_names: [str] = [] for page in page_iterator: for schedule in page["imageIds"]: image_names.append(schedule["imageTag"]) print(f"{len(image_names)} image(s) retrieved.") for schedule_name in image_names: print(f"\t{schedule_name}") if __name__ == "__main__": parser = argparse.ArgumentParser(description="Run hello Amazon ECR.") parser.add_argument( "--repository-name", type=str, help="the name of an Amazon ECR repository in your account.", required=True, ) args = parser.parse_args() hello_ecr(boto3.client("ecr"), args.repository_name)
  • 如需 API 詳細資訊,請參閱 AWS SDK for Python (Boto3) API 參考中的 listImages

下列程式碼範例示範如何使用 Amazon ECR。

Java
SDK for Java 2.x
注意

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

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecr.EcrClient; import software.amazon.awssdk.services.ecr.model.EcrException; import software.amazon.awssdk.services.ecr.model.ListImagesRequest; import software.amazon.awssdk.services.ecr.paginators.ListImagesIterable; public class HelloECR { public static void main(String[] args) { final String usage = """ Usage: <repositoryName> Where: repositoryName - The name of the Amazon ECR repository. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String repoName = args[0]; EcrClient ecrClient = EcrClient.builder() .region(Region.US_EAST_1) .build(); listImageTags(ecrClient, repoName); } public static void listImageTags(EcrClient ecrClient, String repoName){ ListImagesRequest listImagesPaginator = ListImagesRequest.builder() .repositoryName(repoName) .build(); ListImagesIterable imagesIterable = ecrClient.listImagesPaginator(listImagesPaginator); imagesIterable.stream() .flatMap(r -> r.imageIds().stream()) .forEach(image -> System.out.println("The docker image tag is: " +image.imageTag())); } }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 listImages

Kotlin
SDK for Kotlin
注意

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

import aws.sdk.kotlin.services.ecr.EcrClient import aws.sdk.kotlin.services.ecr.model.ListImagesRequest import kotlin.system.exitProcess suspend fun main(args: Array<String>) { val usage = """ Usage: <repositoryName> Where: repositoryName - The name of the Amazon ECR repository. """.trimIndent() if (args.size != 1) { println(usage) exitProcess(1) } val repoName = args[0] listImageTags(repoName) } suspend fun listImageTags(repoName: String?) { val listImages = ListImagesRequest { repositoryName = repoName } EcrClient { region = "us-east-1" }.use { ecrClient -> val imageResponse = ecrClient.listImages(listImages) imageResponse.imageIds?.forEach { imageId -> println("Image tag: ${imageId.imageTag}") } } }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Kotlin API 參考中的 listImages

Python
SDK for Python (Boto3)
注意

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

import boto3 import argparse from boto3 import client def hello_ecr(ecr_client: client, repository_name: str) -> None: """ Use the AWS SDK for Python (Boto3) to create an Amazon Elastic Container Registry (Amazon ECR) client and list the images in a repository. This example uses the default settings specified in your shared credentials and config files. :param ecr_client: A Boto3 Amazon ECR Client object. This object wraps the low-level Amazon ECR service API. :param repository_name: The name of an Amazon ECR repository in your account. """ print( f"Hello, Amazon ECR! Let's list some images in the repository '{repository_name}':\n" ) paginator = ecr_client.get_paginator("list_images") page_iterator = paginator.paginate( repositoryName=repository_name, PaginationConfig={"MaxItems": 10} ) image_names: [str] = [] for page in page_iterator: for schedule in page["imageIds"]: image_names.append(schedule["imageTag"]) print(f"{len(image_names)} image(s) retrieved.") for schedule_name in image_names: print(f"\t{schedule_name}") if __name__ == "__main__": parser = argparse.ArgumentParser(description="Run hello Amazon ECR.") parser.add_argument( "--repository-name", type=str, help="the name of an Amazon ECR repository in your account.", required=True, ) args = parser.parse_args() hello_ecr(boto3.client("ecr"), args.repository_name)
  • 如需 API 詳細資訊,請參閱 AWS SDK for Python (Boto3) API 參考中的 listImages

SDK for Java 2.x
注意

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

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ecr.EcrClient; import software.amazon.awssdk.services.ecr.model.EcrException; import software.amazon.awssdk.services.ecr.model.ListImagesRequest; import software.amazon.awssdk.services.ecr.paginators.ListImagesIterable; public class HelloECR { public static void main(String[] args) { final String usage = """ Usage: <repositoryName> Where: repositoryName - The name of the Amazon ECR repository. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String repoName = args[0]; EcrClient ecrClient = EcrClient.builder() .region(Region.US_EAST_1) .build(); listImageTags(ecrClient, repoName); } public static void listImageTags(EcrClient ecrClient, String repoName){ ListImagesRequest listImagesPaginator = ListImagesRequest.builder() .repositoryName(repoName) .build(); ListImagesIterable imagesIterable = ecrClient.listImagesPaginator(listImagesPaginator); imagesIterable.stream() .flatMap(r -> r.imageIds().stream()) .forEach(image -> System.out.println("The docker image tag is: " +image.imageTag())); } }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考中的 listImages

下一個主題:

基本概念

上一個主題:

使用 AWS SDKs
隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。