本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
CreateRepository
搭配 AWS SDK或 使用 CLI
下列程式碼範例示範如何使用 CreateRepository
。
動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:
- CLI
-
- AWS CLI
-
範例 1:建立儲存庫
下列
create-repository
範例會在帳戶預設登錄檔中指定的命名空間內建立儲存庫。aws ecr create-repository \ --repository-name
project-a/sample-repo
輸出:
{ "repository": { "registryId": "123456789012", "repositoryName": "project-a/sample-repo", "repositoryArn": "arn:aws:ecr:us-west-2:123456789012:repository/project-a/sample-repo" } }
如需詳細資訊,請參閱《Amazon ECR使用者指南》中的建立儲存庫。
範例 2:建立以影像標籤不可變性設定的儲存庫
下列
create-repository
範例會在 帳戶的預設登錄檔中建立針對標籤不可變性設定的儲存庫。aws ecr create-repository \ --repository-name
project-a/sample-repo
\ --image-tag-mutabilityIMMUTABLE
輸出:
{ "repository": { "registryId": "123456789012", "repositoryName": "project-a/sample-repo", "repositoryArn": "arn:aws:ecr:us-west-2:123456789012:repository/project-a/sample-repo", "imageTagMutability": "IMMUTABLE" } }
如需詳細資訊,請參閱《Amazon ECR使用者指南》中的映像標籤互斥性。
範例 3:建立使用掃描組態設定的儲存庫
下列
create-repository
範例會建立儲存庫,該儲存庫設定為在 帳戶的預設登錄檔中的影像推送上執行漏洞掃描。aws ecr create-repository \ --repository-name
project-a/sample-repo
\ --image-scanning-configurationscanOnPush=true
輸出:
{ "repository": { "registryId": "123456789012", "repositoryName": "project-a/sample-repo", "repositoryArn": "arn:aws:ecr:us-west-2:123456789012:repository/project-a/sample-repo", "imageScanningConfiguration": { "scanOnPush": true } } }
如需詳細資訊,請參閱《Amazon ECR使用者指南》中的映像掃描。
-
如需API詳細資訊,請參閱 AWS CLI 命令參考CreateRepository
中的 。
-
- Java
-
- SDK 適用於 Java 2.x
-
注意
還有更多功能 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /** * Creates an Amazon Elastic Container Registry (Amazon ECR) repository. * * @param repoName the name of the repository to create. * @return the Amazon Resource Name (ARN) of the created repository, or an empty string if the operation failed. * @throws IllegalArgumentException If repository name is invalid. * @throws RuntimeException if an error occurs while creating the repository. */ public String createECRRepository(String repoName) { if (repoName == null || repoName.isEmpty()) { throw new IllegalArgumentException("Repository name cannot be null or empty"); } CreateRepositoryRequest request = CreateRepositoryRequest.builder() .repositoryName(repoName) .build(); CompletableFuture<CreateRepositoryResponse> response = getAsyncClient().createRepository(request); try { CreateRepositoryResponse result = response.join(); if (result != null) { System.out.println("The " + repoName + " repository was created successfully."); return result.repository().repositoryArn(); } else { throw new RuntimeException("Unexpected response type"); } } catch (CompletionException e) { Throwable cause = e.getCause(); if (cause instanceof EcrException ex) { if ("RepositoryAlreadyExistsException".equals(ex.awsErrorDetails().errorCode())) { System.out.println("The Amazon ECR repository already exists, moving on..."); DescribeRepositoriesRequest describeRequest = DescribeRepositoriesRequest.builder() .repositoryNames(repoName) .build(); DescribeRepositoriesResponse describeResponse = getAsyncClient().describeRepositories(describeRequest).join(); return describeResponse.repositories().get(0).repositoryArn(); } else { throw new RuntimeException(ex); } } else { throw new RuntimeException(e); } } }
-
如需API詳細資訊,請參閱 AWS SDK for Java 2.x API 參考CreateRepository中的 。
-
- Kotlin
-
- SDK 適用於 Kotlin
-
注意
還有更多功能 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 /** * Creates an Amazon Elastic Container Registry (Amazon ECR) repository. * * @param repoName the name of the repository to create. * @return the Amazon Resource Name (ARN) of the created repository, or an empty string if the operation failed. * @throws RepositoryAlreadyExistsException if the repository exists. * @throws EcrException if an error occurs while creating the repository. */ suspend fun createECRRepository(repoName: String?): String? { val request = CreateRepositoryRequest { repositoryName = repoName } return try { EcrClient { region = "us-east-1" }.use { ecrClient -> val response = ecrClient.createRepository(request) response.repository?.repositoryArn } } catch (e: RepositoryAlreadyExistsException) { println("Repository already exists: $repoName") repoName?.let { getRepoARN(it) } } catch (e: EcrException) { println("An error occurred: ${e.message}") null } }
-
如需API詳細資訊,請參閱 CreateRepository
中的 AWS SDK Kotlin API參考。
-
如需開發人員指南和程式碼範例的完整清單 AWS SDK,請參閱 ECR 搭配 使用 Amazon AWS SDK。本主題也包含入門的相關資訊,以及先前SDK版本的詳細資訊。