Google Cloud BigQuery Data Policies

0.9.0 · active · verified Fri Apr 17

The Google Cloud BigQuery Data Policies API client library provides programmatic access to manage data policies (e.g., column-level security or data masking) within BigQuery datasets. This library allows users to create, retrieve, update, and delete data policies, enforcing fine-grained access control on BigQuery data. It is currently at version 0.9.0 and is part of the `google-cloud-python` monorepo, receiving updates aligned with the underlying API and shared client library components.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes the BigQuery Data Policies client and lists all data policies within a specified Google Cloud project and location. Remember to authenticate your application (e.g., via `gcloud auth application-default login` or `GOOGLE_APPLICATION_CREDENTIALS`).

import os
from google.cloud import bigquery_datapolicies_v1
from google.api_core.exceptions import GoogleAPIError

def list_bigquery_data_policies(project_id: str, location: str):
    """Lists BigQuery data policies for a given project and location."""
    # Ensure GOOGLE_CLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS
    # or other authentication methods are set in your environment.
    client = bigquery_datapolicies_v1.DataPolicyServiceClient()
    parent = f"projects/{project_id}/locations/{location}"

    print(f"Listing data policies for {parent}:")
    try:
        response = client.list_data_policies(parent=parent)
        policies_found = False
        for data_policy in response:
            print(f"  - Data Policy: {data_policy.name}")
            policies_found = True
        if not policies_found:
            print("  No data policies found.")
    except GoogleAPIError as e:
        print(f"Error listing data policies: {e}")

# Example usage:
# Replace with your actual project ID and desired location
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "your-gcp-project-id")
LOCATION = os.environ.get("GCP_REGION", "us-central1") # e.g., "us-central1", "europe-west2"

if PROJECT_ID == "your-gcp-project-id":
    print("Please set the GOOGLE_CLOUD_PROJECT environment variable or replace 'your-gcp-project-id' in the script.")
else:
    list_bigquery_data_policies(PROJECT_ID, LOCATION)

view raw JSON →