本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
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 for 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 for 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 詳細資訊,請參閱 AWS SDK for Kotlin API 參考中的 CreateRepository
。
-
- Python
-
- SDK for Python (Boto3)
-
注意
GitHub 上提供更多範例。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 class ECRWrapper: def __init__(self, ecr_client: client): self.ecr_client = ecr_client @classmethod def from_client(cls) -> "ECRWrapper": """ Creates a ECRWrapper instance with a default Amazon ECR client. :return: An instance of ECRWrapper initialized with the default Amazon ECR client. """ ecr_client = boto3.client("ecr") return cls(ecr_client) def create_repository(self, repository_name: str) -> dict[str, any]: """ Creates an ECR repository. :param repository_name: The name of the repository to create. :return: A dictionary of the created repository. """ try: response = self.ecr_client.create_repository(repositoryName=repository_name) return response["repository"] except ClientError as err: if err.response["Error"]["Code"] == "RepositoryAlreadyExistsException": print(f"Repository {repository_name} already exists.") response = self.ecr_client.describe_repositories( repositoryNames=[repository_name] ) return self.describe_repositories([repository_name])[0] else: logger.error( "Error creating repository %s. Here's why %s", repository_name, err.response["Error"]["Message"], ) raise
-
如需 API 詳細資訊,請參閱 AWS SDK for Python (Boto3) API 參考中的 CreateRepository。
-
如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱 搭配 AWS SDK 使用 Amazon ECR。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。