
AWS CLI CodeArtifactget-repository-endpoint命令详解获取仓库各包格式的 URL 端点【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli本篇文章以 AWS CLI 官方示例文档 为核心结合当前仓库中 CodeArtifact 服务模型 service-2.json 与login命令源码系统讲解aws codeartifact get-repository-endpoint的用途、全部参数、输出结构、端点的 URL 组成规律以及它在连接仓库、配置包管理器这一完整流程中的位置。读完本文你将能够针对 npm、PyPI、Maven、NuGet 等任意包格式准确查询仓库端点并将其接入aws codeartifact login、get-authorization-token等配套命令完成客户端配置。一、命令作用返回仓库针对指定包格式的 URL 端点在 AWS CodeArtifact 中一个仓库会同时托管多种包格式package format的制品而每种包格式都有自己独立的访问端点endpoint。get-repository-endpoint的作用就是返回某个域domain下的某个仓库repository针对指定包格式的 URL。根据仓库中的服务模型定义该命令对应的底层操作说明如下见 service-2.json 中GetRepositoryEndpoint操作的documentation字段Returns the endpoint of a repository for a specific package format. A repository has one endpoint for each package format.即每个仓库对每种包格式都恰好有一个端点支持格式包括cargo、generic、maven、npm、nuget、pypi、ruby、swift八种。该端点 URL 是后续一切连接客户端操作的基础无论是用npm config set registry、pip config、mvn settings.xml还是直接用 AWS CLI 的login命令最终都要把这个 URL 写入包管理器的配置。二、核心示例来自官方示例文档get-repository-endpoint.rst中的标准用法如下查询test-domain域中test-repo仓库的npm端点。aws codeartifact get-repository-endpoint \ --domain test-domain \ --repository test-repo \ --format npm输出结果{ repositoryEndpoint: https://test-domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/npm/test-repo/ }repositoryEndpoint字段就是该仓库 npm 格式的完整 URL。将这个 URL 配置给 npm 客户端即可拉取/推送该仓库中的 npm 包。三、全部参数详解基于服务模型依据 service-2.json 中GetRepositoryEndpointRequest结构的定义该命令共支持 5 个参数其中 3 个为必填参数类型必填说明校验规则--domainDomainName是包含目标仓库的 CodeArtifact 域名称长度 2–50 字符模式[a-z][a-z0-9\-]{0,48}[a-z0-9]--repositoryRepositoryName是仓库名称长度 2–100 字符模式[A-Za-z0-9][A-Za-z0-9._\-]{1,99}--formatPackageFormat是要查询的包格式枚举npm、pypi、maven、nuget、generic、ruby、swift、cargo--domain-ownerAccountId否拥有该域的 AWS 账户 12 位账号 ID不含短横线或空格恰好 12 位数字模式[0-9]{12}--endpoint-typeEndpointType否端点类型枚举dualstack、ipv43.1--format支持的八种包格式PackageFormat枚举定义在 service-2.json 中PackageFormat: {type: string, enum: [npm, pypi, maven, nuget, generic, ruby, swift, cargo]}npmNode.js 生态配合npm/yarn/pnpm使用pypiPython 生态配合pip/poetry/twine使用mavenJVM 生态配合mvn/gradle使用nuget.NET 生态配合dotnet使用generic通用格式适合存放任意类型的制品如 zip 包、二进制文件rubyRubyGems 生态配合gem/bundle使用swiftSwift 包管理器生态配合 Swift Package Manager 使用cargoRust 生态配合cargo使用。注意即使仓库尚未存放任何该格式的包只要仓库创建成功其各格式端点即已存在可以正常查询。3.2--domain-owner跨账户查询默认情况下该命令作用于当前凭证所属账户的域。若仓库位于其他 AWS 账户拥有的域中需要通过--domain-owner显式传入 12 位账户 ID。它与create-repository示例见 create-repository.rst中出现的--domain-owner 111122223333是同一个语义。3.3--endpoint-typedualstack 与 ipv4当仓库需要支持通过 IPv6 访问例如使用 dualstack 类型的 VPC 端点时可指定--endpoint-type dualstack默认不传该参数返回常规的 IPv4 端点。四、端点 URL 的结构规律从输出结果可以看出CodeArtifact 仓库端点的 URL 具有固定结构https://domain-account-id.d.codeartifact.region.amazonaws.com/format/repository/以上文输出为例拆解https://test-domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/npm/test-repo/URL 片段含义来源test-domain域名称--domain111122223333域所属账户的 12 位 ID凭证对应的账户或--domain-ownerus-west-2区域当前 CLI 配置的区域npm包格式--formattest-repo仓库名称--repository该结构并非示例巧合在 login.rst 中aws codeartifact login命令的输出同样打印了完全一致的端点 URLhttps://test-domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/npm/test-repo/可作为结构规律的交叉印证。五、响应字段说明GetRepositoryEndpointResult结构见 service-2.json只包含一个字段字段类型说明repositoryEndpointString返回端点的完整 URL实用技巧当你在脚本中只需要裸 URL不带引号、不带 JSON 包装时可结合--query与--output直接提取aws codeartifact get-repository-endpoint \ --domain test-domain \ --repository test-repo \ --format npm \ --query repositoryEndpoint \ --output text这与 get-authorization-token.rst 中通过--query authorizationToken --output text提取令牌的做法一致便于将输出直接赋给环境变量或写入配置文件。六、在连接仓库流程中的位置与login、get-authorization-token的配合获取端点只是第一步完整的连接流程通常包含获取端点 获取令牌 配置客户端。仓库中的login命令正是这一流程的自动化封装。6.1login命令内部依赖端点查看 login.pyBaseLogin类的构造函数第 55–60 行接收的参数包括def __init__(self, auth_token, expiration, repository_endpoint, domain, repository, subprocess_utils, namespaceNone): self.auth_token auth_token self.expiration expiration self.repository_endpoint repository_endpoint也就是说login在底层需要拿到repository_endpoint即get-repository-endpoint的返回值才能把包管理器配置到正确的仓库地址上。手动执行get-repository-endpoint的本质就是把login自动完成的获取端点这一步拆出来单独做适用于login不支持的工具或需要精细控制配置文件的场景。6.2login的自动化方式如果不想手动拼接端点直接使用login即可示例见 login.rstaws codeartifact login \ --domain test-domain \ --repository test-repo \ --tool npm输出示例Successfully configured npm to use AWS CodeArtifact repository https://test-domain-111122223333.d.codeartifact.us-west-2.amazonaws.com/npm/test-repo/ Login expires in 12 hours at 2020-11-12 01:53:16-05:00可见login内部既完成了端点获取也完成了令牌获取与包管理器配置。6.3 手动配置方式对于login不直接支持的工具或脚本化场景典型的手动流程是# 1. 获取端点 ENDPOINT$(aws codeartifact get-repository-endpoint \ --domain test-domain \ --repository test-repo \ --format pypi \ --query repositoryEndpoint \ --output text) # 2. 获取授权令牌12 小时有效 TOKEN$(aws codeartifact get-authorization-token \ --domain test-domain \ --query authorizationToken \ --output text) # 3. 将端点与令牌写入包管理器配置以 pip 为例 pip config set global.index-url $ENDPOINT pip config set global.extra-index-url https://pypi.org/simple七、底层原理该命令对应的 HTTP 请求从服务模型 service-2.json 的operations.GetRepositoryEndpoint定义可以看到{ name: GetRepositoryEndpoint, http: { method: GET, requestUri: /v1/repository/endpoint }, input: {shape: GetRepositoryEndpointRequest}, output: {shape: GetRepositoryEndpointResult} }即该命令实际发起一个HTTP GET 请求到/v1/repository/endpointdomain、repository、format等参数均以 query string 形式传递location: querystring。这也是该命令无副作用、可安全重复调用的根本原因——它只是一个只读查询操作。八、常见错误与排查根据服务模型中的errors定义GetRepositoryEndpoint可能返回以下异常对应 HTTP 状态码见 service-2.json 中各异常的error.httpStatusCode异常HTTP 状态码典型触发场景ValidationException400--format传了枚举之外的值如docker、--domain或--repository不符合命名规则AccessDeniedException403当前凭证无权访问该域/仓库或跨账户查询时--domain-owner传错ResourceNotFoundException404指定的domain/repository不存在或区域错误ThrottlingException429请求过于频繁被限流可等待retryAfterSeconds后再试InternalServerException500CodeArtifact 服务内部错误常见排查路径检查区域端点 URL 中的区域必须与仓库所在区域一致可通过--region显式指定检查账户跨账户场景必须带--domain-owner检查格式拼写--format为小写枚举值如npm、pypi、maven注意不要写成NPM或PyPI。九、结合本仓库继续深入官方 CLI 示例查看 get-repository-endpoint.rst 原文服务模型完整的参数、枚举、响应与异常定义见 service-2.json配套示例login.rst、get-authorization-token.rst、create-repository.rst自动化封装实现login.py 中BaseLogin对repository_endpoint的消费逻辑。更多关于连接仓库的完整说明可查阅 AWS CodeArtifact User Guide 的 Connect to a repository 章节。【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考