Google Cloud Storage Control
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
- breaking The `google-cloud-storage-control` library transitioned from version `0.x.x` to `1.x.x` (released 2023-08-09). This major version bump primarily adopted the Storage Control API v2. Users migrating from `0.x.x` might encounter breaking changes in client initialization, method signatures, and resource representations.
- gotcha Managed Folders are a new feature and require specific IAM permissions beyond standard bucket access. Incorrect or insufficient permissions can lead to `PermissionDenied` errors.
- gotcha Resource paths for operations (e.g., `parent` for `list_managed_folders`) must follow a strict format like `projects/{project_id}/buckets/{bucket_name}`.
Install
-
pip install google-cloud-storage-control
Imports
- StorageControlClient
from google.cloud.storage_control_v2 import StorageControlClient
- ManagedFolder
from google.cloud.storage_control_v2.types import ManagedFolder
Quickstart
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.")