または AWS SDK CreateRepositoryで使用する CLI - AWS SDK コード例

AWS Doc SDK Examples GitHub リポジトリには他にも AWS SDK例があります。

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

または AWS SDK CreateRepositoryで使用する 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-mutability IMMUTABLE

出力:

{ "repository": { "registryId": "123456789012", "repositoryName": "project-a/sample-repo", "repositoryArn": "arn:aws:ecr:us-west-2:123456789012:repository/project-a/sample-repo", "imageTagMutability": "IMMUTABLE" } }

詳細については、「Amazon ユーザーガイド」の「Image Tag Mutability」を参照してください。 ECR

例 3: スキャン設定で設定されたリポジトリを作成するには

次のcreate-repository例では、アカウントのデフォルトレジストリでイメージプッシュに対して脆弱性スキャンを実行するように構成されたリポジトリを作成します。

aws ecr create-repository \ --repository-name project-a/sample-repo \ --image-scanning-configuration scanOnPush=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 詳細については、 リファレンスCreateRepositoryの「」を参照してください。 AWS SDK for Java 2.x API

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 詳細については、Kotlin リファレンス のCreateRepository「」の「」を参照してください。 AWS SDK API