Code examples for Amazon ECR using AWS SDKs - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Code examples for Amazon ECR using AWS SDKs

The following code examples show you how to use Amazon Elastic Container Registry (Amazon ECR) with an AWS software development kit (SDK).

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

More resources

Get started

The following code examples show how to get started using Amazon ECR.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

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())); } }
  • For API details, see listImages in AWS SDK for Java 2.x API Reference.

Kotlin
SDK for Kotlin
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

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}") } } }
  • For API details, see listImages in AWS SDK for Kotlin API reference.