Skip to main content

Google Cloud Storage client library for kiarina namespace

Project description

kiarina-lib-google-cloud-storage

A Python client library for Google Cloud Storage with configuration management using pydantic-settings-manager.

Purpose

This library separates infrastructure configuration (bucket names, blob paths, credentials) from application code, enabling:

  • Multi-environment deployment with the same codebase
  • Multi-tenant applications with isolated storage
  • Testable code without hard-coded infrastructure details
  • Blob path structures aligned with GCS security rules

Installation

pip install kiarina-lib-google-cloud-storage

Quick Start

from kiarina.lib.google.cloud_storage import get_blob, settings_manager

# Configure storage settings
settings_manager.user_config = {
    "default": {
        "bucket_name": "my-app-data",
        "blob_name_pattern": "users/{user_id}/files/{basename}"
    }
}

# Get blob with placeholders
blob = get_blob(placeholders={
    "user_id": "123",
    "basename": "profile.json"
})
# Actual path: gs://my-app-data/users/123/files/profile.json

# Use native google-cloud-storage API
blob.upload_from_string("Hello, World!")
content = blob.download_as_text()

API Reference

get_storage_client()

Get an authenticated Google Cloud Storage client.

def get_storage_client(
    auth_settings_key: str | None = None,
    **kwargs: Any
) -> storage.Client

Parameters:

  • auth_settings_key: Configuration key for kiarina-lib-google-auth (default: None)
  • **kwargs: Additional arguments passed to storage.Client()

Returns: storage.Client - Authenticated Google Cloud Storage client

Example:

from kiarina.lib.google.cloud_storage import get_storage_client

client = get_storage_client()
client = get_storage_client(auth_settings_key="production")

get_bucket()

Get a Google Cloud Storage bucket.

def get_bucket(
    settings_key: str | None = None,
    *,
    auth_settings_key: str | None = None,
    **kwargs: Any
) -> storage.Bucket

Parameters:

  • settings_key: Configuration key for storage settings (default: None)
  • auth_settings_key: Configuration key for authentication (default: None)
  • **kwargs: Additional arguments passed to get_storage_client()

Returns: storage.Bucket - Google Cloud Storage bucket

Example:

from kiarina.lib.google.cloud_storage import get_bucket

bucket = get_bucket()
bucket = get_bucket(settings_key="production", auth_settings_key="prod_auth")

# Use native google-cloud-storage API
for blob in bucket.list_blobs(prefix="users/"):
    print(blob.name)

get_blob()

Get a Google Cloud Storage blob.

def get_blob(
    blob_name: str | None = None,
    *,
    placeholders: dict[str, Any] | None = None,
    settings_key: str | None = None,
    auth_settings_key: str | None = None,
    **kwargs: Any
) -> storage.Blob

Parameters:

  • blob_name: Full blob path (default: None)
  • placeholders: Placeholders for blob_name_pattern formatting (default: None)
  • settings_key: Configuration key for storage settings (default: None)
  • auth_settings_key: Configuration key for authentication (default: None)
  • **kwargs: Additional arguments passed to get_bucket()

Returns: storage.Blob - Google Cloud Storage blob

Blob Name Resolution Priority:

  1. Explicit blob_name parameter
  2. blob_name_pattern with placeholders
  3. blob_name_pattern without placeholders (fixed name)

Example:

from kiarina.lib.google.cloud_storage import get_blob

# Direct blob name
blob = get_blob(blob_name="users/123/profile.json")

# Using pattern with placeholders
blob = get_blob(placeholders={"user_id": "123", "basename": "profile.json"})

# Using fixed pattern from settings
blob = get_blob()

# Use native google-cloud-storage API
blob.upload_from_string("content")
content = blob.download_as_text()
blob.delete()

GoogleCloudStorageSettings

Pydantic settings model for Google Cloud Storage configuration.

Fields:

  • bucket_name (str, required): Google Cloud Storage bucket name
  • blob_name_pattern (str | None, optional): Blob name pattern with placeholders (e.g., "users/{user_id}/files/{basename}")

Example:

from kiarina.lib.google.cloud_storage import GoogleCloudStorageSettings

settings = GoogleCloudStorageSettings(
    bucket_name="my-bucket",
    blob_name_pattern="users/{user_id}/files/{basename}"
)

settings_manager

Global settings manager instance for managing multiple configurations.

Type: SettingsManager[GoogleCloudStorageSettings]

Example:

from kiarina.lib.google.cloud_storage import settings_manager

# Single configuration
settings_manager.user_config = {
    "bucket_name": "my-bucket",
    "blob_name_pattern": "files/{basename}"
}

# Multiple configurations
settings_manager.user_config = {
    "production": {
        "bucket_name": "prod-bucket",
        "blob_name_pattern": "v2/prod/{basename}"
    },
    "staging": {
        "bucket_name": "staging-bucket",
        "blob_name_pattern": "v2/staging/{basename}"
    }
}

Configuration

YAML Configuration

# config/production.yaml
kiarina.lib.google.auth:
  default:
    type: service_account
    service_account_file: /secrets/prod-sa-key.json

kiarina.lib.google.cloud_storage:
  default:
    bucket_name: prod-us-west1-app-data
    blob_name_pattern: "v2/production/{tenant_id}/users/{user_id}/files/{basename}"

Loading Configuration:

import yaml
from pydantic_settings_manager import load_user_configs

with open("config/production.yaml") as f:
    config = yaml.safe_load(f)
load_user_configs(config)

# Now all modules are configured
from kiarina.lib.google.cloud_storage import get_blob
blob = get_blob(placeholders={
    "tenant_id": "acme",
    "user_id": "123",
    "basename": "data.json"
})

Multiple Environments:

# config/production.yaml
kiarina.lib.google.cloud_storage:
  default:
    bucket_name: prod-us-west1-app-data
    blob_name_pattern: "v2/production/{basename}"

# config/staging.yaml
kiarina.lib.google.cloud_storage:
  default:
    bucket_name: staging-app-data
    blob_name_pattern: "v2/staging/{basename}"

# config/development.yaml
kiarina.lib.google.cloud_storage:
  default:
    bucket_name: dev-local-data
    blob_name_pattern: "v2/dev/{basename}"

Multi-Tenant Configuration:

kiarina.lib.google.cloud_storage:
  tenant_acme:
    bucket_name: acme-corp-data
    blob_name_pattern: "app-data/{basename}"
  tenant_globex:
    bucket_name: globex-data
    blob_name_pattern: "app-data/{basename}"

Blob Name Pattern Examples:

# Fixed name
blob_name_pattern: "data.json"

# Single placeholder
blob_name_pattern: "files/{basename}"

# Multiple placeholders
blob_name_pattern: "my-service/{tenant_id}/users/{user_id}/files/{basename}"

# Hierarchical structure
blob_name_pattern: "v2/{environment}/{service}/{tenant_id}/{resource_type}/{resource_id}/{basename}"

Environment Variables:

All settings can be configured via environment variables with the KIARINA_LIB_GOOGLE_CLOUD_STORAGE_ prefix:

export KIARINA_LIB_GOOGLE_CLOUD_STORAGE_BUCKET_NAME="my-bucket"
export KIARINA_LIB_GOOGLE_CLOUD_STORAGE_BLOB_NAME_PATTERN="users/{user_id}/files/{basename}"

Configuration Priority:

  1. CLI arguments (via settings_manager.cli_args)
  2. Environment variables
  3. User configuration (via settings_manager.user_config)
  4. Default values

Testing

Running Tests

# Run tests (requires test configuration)
mise run package:test kiarina-lib-google-cloud-storage

# With coverage
mise run package:test kiarina-lib-google-cloud-storage --coverage

Test Configuration

  1. Copy sample files:

    cp .env.sample .env
    cp test_settings.sample.yaml test_settings.yaml
    
  2. Edit test_settings.yaml with your GCS configuration:

    kiarina.lib.google.auth:
      service_account:
        type: service_account
        service_account_file: ~/.gcp/service-account/your-project/key.json
    
    kiarina.lib.google.cloud_storage:
      default:
        bucket_name: your-test-bucket
        blob_name_pattern: "test/{tenant_id}/users/{user_id}/files/{basename}"
    
  3. Set environment variable:

    export KIARINA_LIB_GOOGLE_CLOUD_STORAGE_TEST_SETTINGS_FILE=/path/to/test_settings.yaml
    

Writing Tests

# tests/conftest.py
import pytest
from kiarina.lib.google.cloud_storage import settings_manager

@pytest.fixture
def storage_config():
    settings_manager.user_config = {
        "test": {
            "bucket_name": "test-bucket",
            "blob_name_pattern": "test-run/{basename}"
        }
    }

# tests/test_user_service.py
def test_save_user_profile(storage_config):
    from myapp.services import save_user_profile
    save_user_profile("user123", {"name": "Alice"})
    
    from kiarina.lib.google.cloud_storage import get_blob
    blob = get_blob(placeholders={"basename": "users/user123/profile.json"})
    assert blob.exists()

Dependencies

License

This project is licensed under the MIT License - see the LICENSE file for details.

Related Projects

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kiarina_lib_google_cloud_storage-1.22.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file kiarina_lib_google_cloud_storage-1.22.1.tar.gz.

File metadata

File hashes

Hashes for kiarina_lib_google_cloud_storage-1.22.1.tar.gz
Algorithm Hash digest
SHA256 bcb61a569520fb2d1dd72f02423ff16dd502b0be6f09b36e63c7cd2eb16b9d51
MD5 99cea7aff9de287d8c42702dc2da811f
BLAKE2b-256 bce3f832486f88ffa5afc6cb1954efdb0a4ef8a13a53961c844f71d2349055aa

See more details on using hashes here.

File details

Details for the file kiarina_lib_google_cloud_storage-1.22.1-py3-none-any.whl.

File metadata

File hashes

Hashes for kiarina_lib_google_cloud_storage-1.22.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a2c20497f490be5190ea656ee4d5fa0823b7e074b58f1aa3af946752052fa494
MD5 626e7a23edb1ae375e629bad8d6b990b
BLAKE2b-256 b2b4d27d98762bda701aeab77b5941d6d791370e0985e915ff99ed35d1db94b5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page