google.api_core.exceptions.InvalidArgument: 400 无效的存储库

huangapple go评论56阅读模式
英文:

google.api_core.exceptions.InvalidArgument: 400 invalid repository

问题

I'm here to provide the translation of the code and relevant information:

from google.cloud import artifactregistry_v1

def sample_create_repository():
    # 创建客户端
    print("正在测试代码")

    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("等待操作完成...")

    response = operation.result()

    # 处理响应
    print(response)

sample_create_repository()

你遇到的错误是因为 repository 参数的格式不正确。确保它的值与项目、位置、存储库名称和格式的格式与你的项目配置匹配。如果需要更多帮助,请参考相关文档。

英文:

I am trying to create a repository in Artifact registry of GCP using Python API by following below link.

https://cloud.google.com/python/docs/reference/artifactregistry/latest/google.cloud.artifactregistry_v1.services.artifact_registry.ArtifactRegistryClient#google_cloud_artifactregistry_v1_services_artifact_registry_ArtifactRegistryClient_create_repository

Below is the code I am using.

from google.cloud import artifactregistry_v1

def sample_create_repository():
    # Create a client

    print(&quot;Testing the code&quot;)

    client = artifactregistry_v1.ArtifactRegistryClient.from_service_account_file(filename=&#39;&lt;path_to_sa.json&gt;&#39;)

    # Initialize request argument(s)
    
    request = artifactregistry_v1.CreateRepositoryRequest(parent=&quot;projects/p1/locations/us&quot;,repository={&#39;name&#39;:&#39;projects/p1/locations/us/repositories/testrepo&#39;,&#39;format_&#39;:&#39;DOCKER&#39;})

    # Make the request
    operation = client.create_repository(request=request)

    print(&quot;Waiting for operation to complete...&quot;)

    response = operation.result()

    # Handle the response
    print(response)

sample_create_repository()

I am getting below error for this code, Can anyone from the community suggest the correct format here,

    raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.InvalidArgument: 400 invalid repository: generic::unknown: invalid repository name: &quot;projects/p1/locations/us/repositories/&quot;: invalid repository name: projects/p1/locations/us/repositories/

parent format I have taken this doc as reference - https://cloud.google.com/artifact-registry/docs/reference/rest

repository format I have taken this doc as reference - https://cloud.google.com/python/docs/reference/artifactregistry/latest/google.cloud.artifactregistry_v1.types.Repository

答案1

得分: 2

你需要在CreateRepositoryRequest中提供存储库ID。对于Create操作,Repository.Name会被忽略。(大多数Google API都会类似行事,详见https://google.aip.dev/133#user-specified-ids)

英文:

You need to provide the repository id in the CreateRepositoryRequest itself. Repository.Name is ignored for Create. (Most Google apis will behave similarly, see https://google.aip.dev/133#user-specified-ids)

huangapple
  • 本文由 发表于 2023年5月18日 01:15:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定