Google Cloud Artifact Registry
The `google-cloud-artifact-registry` client library for Python allows developers to interact with Google Cloud Artifact Registry. This service provides a universal package manager for various artifact formats, including Docker images, Maven artifacts, npm packages, and more. The library is currently at version 1.21.0 and is part of the actively maintained Google Cloud Python client libraries, receiving frequent updates.
Warnings
- gotcha Artifact Registry resources (repositories, packages, artifacts) are regional. Operations like listing or creating repositories must specify a valid `location` (e.g., 'us-central1', 'europe-west1'). Forgetting this or using an incorrect region will result in 'NotFound' or 'PermissionDenied' errors, even if the resource exists in another region.
- gotcha Many Artifact Registry API calls require fully qualified resource names (e.g., `projects/PROJECT_ID/locations/LOCATION/repositories/REPOSITORY_ID`). Incorrectly formatting these strings is a common source of `INVALID_ARGUMENT` or `NotFound` errors.
- gotcha The client library uses Application Default Credentials for authentication. If your code is running outside of Google Cloud (e.g., on a local machine), you must explicitly authenticate (e.g., via `gcloud auth application-default login`) or set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable. Incorrect setup often leads to `google.auth.exceptions.DefaultCredentialsError` or `PermissionDenied` errors.
Install
-
pip install google-cloud-artifact-registry
Imports
- ArtifactRegistryClient
from google.cloud.artifactregistry_v1 import ArtifactRegistryClient
- ArtifactRegistryAsyncClient
from google.cloud.artifactregistry_v1 import ArtifactRegistryAsyncClient
Quickstart
import os
from google.cloud.artifactregistry_v1 import ArtifactRegistryClient
def list_repositories(project_id: str, location: str):
"""Lists repositories in a given project and location."""
client = ArtifactRegistryClient()
# The parent resource name for the project and location
parent = f"projects/{project_id}/locations/{location}"
try:
print(f"Listing repositories in {parent}...")
# list_repositories returns an iterator, common for Google Cloud APIs
repositories = client.list_repositories(parent=parent)
found = False
for repo in repositories:
print(f"- {repo.name} (Format: {repo.format.name}, Description: {repo.description})")
found = True
if not found:
print("No repositories found.")
except Exception as e:
print(f"Error listing repositories: {e}")
if __name__ == "__main__":
# Set these environment variables or replace the placeholders
PROJECT_ID = os.environ.get("GCP_PROJECT_ID", "your-gcp-project-id")
LOCATION = os.environ.get("GCP_LOCATION", "us-central1") # e.g., 'us-central1', 'europe-west1'
if PROJECT_ID == "your-gcp-project-id":
print("Please set the GCP_PROJECT_ID environment variable or replace 'your-gcp-project-id'.")
print("Also ensure you are authenticated (e.g., `gcloud auth application-default login`).")
else:
list_repositories(PROJECT_ID, LOCATION)