Gunakan PushImageCmd dengan AWS SDK atau CLI - AWS Contoh Kode SDK

Ada lebih banyak contoh AWS SDK yang tersedia di repo Contoh SDK AWS Doc. GitHub

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Gunakan PushImageCmd dengan AWS SDK atau CLI

Contoh kode berikut menunjukkan cara menggunakanPushImageCmd.

Java
SDK untuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

/** * Pushes a Docker image to an Amazon Elastic Container Registry (ECR) repository. * * @param repoName the name of the ECR repository to push the image to. * @param imageName the name of the Docker image. */ public void pushDockerImage(String repoName, String imageName) { System.out.println("Pushing " + imageName + " to Amazon ECR will take a few seconds."); CompletableFuture<AuthConfig> authResponseFuture = getAsyncClient().getAuthorizationToken() .thenApply(response -> { String token = response.authorizationData().get(0).authorizationToken(); String decodedToken = new String(Base64.getDecoder().decode(token)); String password = decodedToken.substring(4); DescribeRepositoriesResponse descrRepoResponse = getAsyncClient().describeRepositories(b -> b.repositoryNames(repoName)).join(); Repository repoData = descrRepoResponse.repositories().stream().filter(r -> r.repositoryName().equals(repoName)).findFirst().orElse(null); assert repoData != null; String registryURL = repoData.repositoryUri().split("/")[0]; AuthConfig authConfig = new AuthConfig() .withUsername("AWS") .withPassword(password) .withRegistryAddress(registryURL); return authConfig; }) .thenCompose(authConfig -> { DescribeRepositoriesResponse descrRepoResponse = getAsyncClient().describeRepositories(b -> b.repositoryNames(repoName)).join(); Repository repoData = descrRepoResponse.repositories().stream().filter(r -> r.repositoryName().equals(repoName)).findFirst().orElse(null); getDockerClient().tagImageCmd(imageName + ":latest", repoData.repositoryUri() + ":latest", imageName).exec(); try { getDockerClient().pushImageCmd(repoData.repositoryUri()).withTag("echo-text").withAuthConfig(authConfig).start().awaitCompletion(); System.out.println("The " + imageName + " was pushed to ECR"); } catch (InterruptedException e) { throw (RuntimeException) e.getCause(); } return CompletableFuture.completedFuture(authConfig); }); authResponseFuture.join(); }
  • Untuk detail API, lihat PushImageCmddi Referensi AWS SDK for Java 2.x API.

Kotlin
SDK untuk Kotlin
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara mengatur dan menjalankannya di AWS Repositori Contoh Kode.

/** * Pushes a Docker image to an Amazon Elastic Container Registry (ECR) repository. * * @param repoName the name of the ECR repository to push the image to. * @param imageName the name of the Docker image. */ suspend fun pushDockerImage( repoName: String, imageName: String, ) { println("Pushing $imageName to $repoName will take a few seconds") val authConfig = getAuthConfig(repoName) EcrClient { region = "us-east-1" }.use { ecrClient -> val desRequest = DescribeRepositoriesRequest { repositoryNames = listOf(repoName) } val describeRepoResponse = ecrClient.describeRepositories(desRequest) val repoData = describeRepoResponse.repositories?.firstOrNull { it.repositoryName == repoName } ?: throw RuntimeException("Repository not found: $repoName") val tagImageCmd = getDockerClient()?.tagImageCmd("$imageName", "${repoData.repositoryUri}", imageName) if (tagImageCmd != null) { tagImageCmd.exec() } val pushImageCmd = repoData.repositoryUri?.let { dockerClient?.pushImageCmd(it) // ?.withTag("latest") ?.withAuthConfig(authConfig) } try { if (pushImageCmd != null) { pushImageCmd.start().awaitCompletion() } println("The $imageName was pushed to Amazon ECR") } catch (e: IOException) { throw RuntimeException(e) } } }
  • Untuk detail API, lihat PushImageCmddi AWS SDK untuk referensi API Kotlin.