Use StartLifecyclePolicyPreview com um AWS SDK ou CLI - AWS Exemplos de código do SDK

Há mais exemplos de AWS SDK disponíveis no repositório AWS Doc SDK Examples GitHub .

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Use StartLifecyclePolicyPreview com um AWS SDK ou CLI

Os exemplos de códigos a seguir mostram como usar StartLifecyclePolicyPreview.

Exemplos de ações são trechos de código de programas maiores e devem ser executados em contexto. É possível ver essa ação no contexto no seguinte exemplo de código:

CLI
AWS CLI

Para criar uma prévia da política de ciclo de vida

O start-lifecycle-policy-preview exemplo a seguir cria uma visualização prévia da política de ciclo de vida definida por um arquivo JSON para o repositório especificado.

aws ecr start-lifecycle-policy-preview \ --repository-name "project-a/amazon-ecs-sample" \ --lifecycle-policy-text "file://policy.json"

Conteúdo de policy.json:

{ "rules": [ { "rulePriority": 1, "description": "Expire images older than 14 days", "selection": { "tagStatus": "untagged", "countType": "sinceImagePushed", "countUnit": "days", "countNumber": 14 }, "action": { "type": "expire" } } ] }

Saída:

{ "registryId": "012345678910", "repositoryName": "project-a/amazon-ecs-sample", "lifecyclePolicyText": "{\n \"rules\": [\n {\n \"rulePriority\": 1,\n \"description\": \"Expire images older than 14 days\",\n \"selection\": {\n \"tagStatus\": \"untagged\",\n \"countType\": \"sinceImagePushed\",\n \"countUnit\": \"days\",\n \"countNumber\": 14\n },\n \"action\": {\n \"type\": \"expire\"\n }\n }\n ]\n}\n", "status": "IN_PROGRESS" }
Java
SDK para Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

/** * Verifies the existence of an image in an Amazon Elastic Container Registry (Amazon ECR) repository asynchronously. * * @param repositoryName The name of the Amazon ECR repository. * @param imageTag The tag of the image to verify. * @throws EcrException if there is an error retrieving the image information from Amazon ECR. * @throws CompletionException if the asynchronous operation completes exceptionally. */ public void verifyImage(String repositoryName, String imageTag) { DescribeImagesRequest request = DescribeImagesRequest.builder() .repositoryName(repositoryName) .imageIds(ImageIdentifier.builder().imageTag(imageTag).build()) .build(); CompletableFuture<DescribeImagesResponse> response = getAsyncClient().describeImages(request); response.whenComplete((describeImagesResponse, ex) -> { if (ex != null) { if (ex instanceof CompletionException) { Throwable cause = ex.getCause(); if (cause instanceof EcrException) { throw (EcrException) cause; } else { throw new RuntimeException("Unexpected error: " + cause.getMessage(), cause); } } else { throw new RuntimeException("Unexpected error: " + ex.getCause()); } } else if (describeImagesResponse != null && !describeImagesResponse.imageDetails().isEmpty()) { System.out.println("Image is present in the repository."); } else { System.out.println("Image is not present in the repository."); } }); // Wait for the CompletableFuture to complete. response.join(); }
Kotlin
SDK para Kotlin
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

/** * Verifies the existence of an image in an Amazon Elastic Container Registry (Amazon ECR) repository asynchronously. * * @param repositoryName The name of the Amazon ECR repository. * @param imageTag The tag of the image to verify. */ suspend fun verifyImage( repoName: String?, imageTagVal: String?, ) { require(!(repoName == null || repoName.isEmpty())) { "Repository name cannot be null or empty" } require(!(imageTagVal == null || imageTagVal.isEmpty())) { "Image tag cannot be null or empty" } val imageId = ImageIdentifier { imageTag = imageTagVal } val request = DescribeImagesRequest { repositoryName = repoName imageIds = listOf(imageId) } EcrClient { region = "us-east-1" }.use { ecrClient -> val describeImagesResponse = ecrClient.describeImages(request) if (describeImagesResponse != null && !describeImagesResponse.imageDetails?.isEmpty()!!) { println("Image is present in the repository.") } else { println("Image is not present in the repository.") } } }