Google Cloud Storage Control

1.11.0 · active · verified Thu Apr 09

The `google-cloud-storage-control` library provides a Python client for interacting with the Google Cloud Storage Control API. This API allows for programmatic management of Storage features like Managed Folders and other bucket-level controls. It is currently at version 1.11.0 and is part of the larger `google-cloud-python` monorepo, receiving frequent updates along with other Google Cloud client libraries.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `StorageControlClient` and list managed folders within a specified Google Cloud Storage bucket. Ensure your `GOOGLE_CLOUD_PROJECT` and `GCS_BUCKET_NAME` environment variables are set, and your environment is authenticated (e.g., via `gcloud auth application-default login`).

import os
from google.cloud.storage_control_v2 import StorageControlClient

project_id = os.environ.get('GOOGLE_CLOUD_PROJECT', 'your-gcp-project-id')
bucket_name = os.environ.get('GCS_BUCKET_NAME', 'your-gcs-bucket-name')

if project_id == 'your-gcp-project-id':
    print("Please set the GOOGLE_CLOUD_PROJECT environment variable or replace 'your-gcp-project-id'.")
    exit()
if bucket_name == 'your-gcs-bucket-name':
    print("Please set the GCS_BUCKET_NAME environment variable or replace 'your-gcs-bucket-name'.")
    exit()

client = StorageControlClient()

parent_path = f"projects/{project_id}/buckets/{bucket_name}"

try:
    # List managed folders in a bucket
    print(f"Listing managed folders in {parent_path}:")
    for managed_folder in client.list_managed_folders(parent=parent_path):
        print(f"- {managed_folder.name}")
except Exception as e:
    print(f"Error listing managed folders: {e}")
    print("Ensure you have appropriate IAM permissions (e.g., 'roles/storage.admin') for the bucket and that Application Default Credentials are configured.")

view raw JSON →