You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GCP Artifact Registry Python API创建仓库遇400参数错误求助

解决GCP Artifact Registry Python API创建仓库的参数错误

问题场景

尝试使用GCP Artifact Registry的Python API创建仓库,运行代码时返回参数无效错误:

google.api_core.exceptions.InvalidArgument: 400 invalid repository: generic::unknown: invalid repository name: "projects/p1/locations/us/repositories/": invalid repository name: projects/p1/locations/us/repositories/

原代码如下:

from google.cloud import artifactregistry_v1

def sample_create_repository():
    print("Testing the code")

    client = artifactregistry_v1.ArtifactRegistryClient.from_service_account_file(filename='<path_to_sa.json>')

    request = artifactregistry_v1.CreateRepositoryRequest(
        parent="projects/p1/locations/us",
        repository={'name':'projects/p1/locations/us/repositories/testrepo','format_':'DOCKER'}
    )

    operation = client.create_repository(request=request)
    print("Waiting for operation to complete...")
    response = operation.result()
    print(response)

sample_create_repository()

错误原因

问题出在repository参数的name字段:

  • 调用create_repository接口时,parent已经指定了父资源路径(projects/p1/locations/us),此时只需给repository提供短仓库名称(如testrepo),无需传入完整资源路径。
  • 手动指定完整name会导致API拼接路径时生成无效格式,触发错误。

修正后的代码

from google.cloud import artifactregistry_v1

def sample_create_repository():
    print("Testing the code")

    client = artifactregistry_v1.ArtifactRegistryClient.from_service_account_file(filename='<path_to_sa.json>')

    # 修正:repository仅需指定短名称和格式,无需完整路径
    request = artifactregistry_v1.CreateRepositoryRequest(
        parent="projects/p1/locations/us-central1",
        repository={'name': 'testrepo', 'format_': 'DOCKER'}
    )

    operation = client.create_repository(request=request)
    print("Waiting for operation to complete...")
    response = operation.result()
    print(response)

sample_create_repository()

补充说明

  • parent中的位置需使用GCP支持的具体区域(如us-central1),原代码中的us是无效区域,会触发额外错误。
  • format_字段可选值包括DOCKER、MAVEN、NPM等,需匹配目标仓库类型。

内容的提问来源于stack exchange,提问作者ash_ketchum12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 17:17:30