简体   繁体   中英

Google artifact registry list_tags in python - contains invalid argument

I'm a code newbie so please be kind:) I've got my google credentials set and I've checked the documentation for the python library for artifact registry (v1), but clearly doing something daft, as I'm receiving this response back - google.api_core.exceptions.InvalidArgument: 400 Request contains an invalid argument.

Here's my code:

from google.cloud import artifactregistry_v1

# Using Application Default Credentails is easiest
# export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json
client = artifactregistry_v1.ArtifactRegistryClient()

# Best obtained from the environment
project = "**REDACTED**" 
location = "europe-west2"
repository = "test"

parent = f"projects/{project}/locations/{location}/repositories/{repository}"

request = artifactregistry_v1.ListTagsRequest(parent=parent)

page_result = client.list_tags(request=request)

for response in page_result:
    print(response)

Here's the official docs, I can't work out what I've done wrong: 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_list_tags

EDIT

Just seen on the Google docs for the Class ListTagsRequest ( here ) it says parent expects a string (which is what i've done), but noticed in PyCharm it's highlighted and telling me expected type 'Dict', and got 'str' instead ....

I was finally able to find the correct code:

from google.cloud import artifactregistry_v1


def get_package_tags_from_artifact_registry():
    # Using Application Default Credentails is easiest
    # export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json
    client = artifactregistry_v1.ArtifactRegistryClient()

    # Best obtained from the environment
    project = "{project_id}"
    location = "{location}"
    repository = "{repository}"
    package_name = "{package_name}"

    parent = f"projects/{project}/locations/{location}/repositories/{repository}/packages/{package_name}"

    request = artifactregistry_v1.ListTagsRequest(
        parent=parent
    )

    page_result = client.list_tags(
        request=request
    )

    for response in page_result:
        print(response)

    return page_result


if __name__ == '__main__':
    result = get_package_tags_from_artifact_registry()

I checked from this doc and tested with a parent path including the package name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM