Skip to main content

Unified AWS S3, Azure Blob Storage, and GCP Cloud Storage client for cloud file uploads

Project description

OS.py Cloud Storage Library

A unified Python library for AWS S3, Azure Blob Storage, and GCP Cloud Storage operations with built-in retry logic, error handling, and CDN verification.

Author: Oleg Smirnov Repository: https://github.com/BestianCode/os.py.lib.cloud

Features

  • AWS S3 Support: Upload files to S3 with automatic retry logic
  • Azure Blob Storage Support: Upload to Azure with CDN verification
  • GCP Cloud Storage Support: Upload to GCP using service account JSON
  • Unified Interface: Consistent API for all storage providers
  • Automatic Retries: Exponential backoff for resilient uploads
  • Environment-based Configuration: Easy setup via environment variables

Installation

From GitHub

pip install git+https://github.com/BestianCode/os.py.lib.cloud.git

From PyPI (once published)

pip install os-py-lib-cloud

Development Installation

git clone https://github.com/BestianCode/os.py.lib.cloud.git
cd os.py.lib.cloud
pip install -e .

Configuration

AWS S3

Set the following environment variables:

export AWS_ACCESS_KEY_ID="your_access_key"
export AWS_SECRET_ACCESS_KEY="your_secret_key"
export AWS_REGION="us-east-1"
export AWS_S3_BUCKET="your-bucket-name"

Azure Blob Storage

export AZURE_STORAGE_CONNECTION_STRING="your_connection_string"
export AZURE_CONTAINER_NAME="your-container-name"

GCP Cloud Storage

export GCP_CREDENTIALS_FILE="/path/to/service-account.json"
export GCP_BUCKET_NAME="your-bucket-name"
export GCP_PROJECT_ID="your-project-id"  # optional

Usage

Basic S3 Example

from os_cloud_storage import os_s3
from pathlib import Path

s3_initialized = os_s3.initialize_s3()

if s3_initialized:
    success, public_url, s3_url = os_s3.upload_to_s3(
        Path("/path/to/file.mp4"),
        s3_key="videos/my-video.mp4"
    )
    if success:
        print(f"Uploaded: {public_url}")

Azure Blob Storage Example

from os_cloud_storage import os_azure
from pathlib import Path

azure_initialized = os_azure.initialize_azure()

if azure_initialized:
    success, public_url, blob_url = os_azure.upload_to_azure(
        Path("/path/to/file.mp4"),
        blob_name="videos/my-video.mp4"
    )
    if success:
        print(f"Uploaded: {public_url}")

GCP Cloud Storage Example

from os_cloud_storage import os_gcp
from pathlib import Path

gcp_initialized = os_gcp.initialize_gcp()

if gcp_initialized:
    success, public_url, gcs_url = os_gcp.upload_to_gcp(
        Path("/path/to/file.mp4"),
        blob_name="videos/my-video.mp4"
    )
    if success:
        print(f"Uploaded: {public_url}")

Multi-Provider Example

from os_cloud_storage import os_s3, os_azure, os_gcp
from pathlib import Path

# Initialize providers
os_s3.initialize_s3()
os_azure.initialize_azure()
os_gcp.initialize_gcp()

# Upload with fallback
def upload_file(file_path):
    if os_s3.is_initialized():
        return os_s3.upload_to_s3(Path(file_path))
    elif os_azure.is_initialized():
        return os_azure.upload_to_azure(Path(file_path))
    elif os_gcp.is_initialized():
        return os_gcp.upload_to_gcp(Path(file_path))
    return False, None, None

API Reference

S3 Module (os_s3)

Functions

  • initialize_s3(file_backend_url: str) -> bool

    • Initialize S3 client with environment variables
    • Returns: True if successful, False otherwise
  • upload_to_s3(local_file_path: Path, s3_key: str = None, max_retries: int = 3, file_backend_url: str = None) -> Tuple[bool, str, str]

    • Upload a file to S3
    • Returns: (success, public_url, s3_url)
  • is_initialized() -> bool

    • Check if S3 is initialized
  • get_s3_client()

    • Get the boto3 S3 client
  • get_bucket_name() -> str

    • Get the configured bucket name
  • get_region() -> str

    • Get the configured AWS region

Azure Module (os_azure)

Functions

  • initialize_azure(file_backend_url: str) -> bool

    • Initialize Azure Blob Storage client with environment variables
    • Returns: True if successful, False otherwise
  • upload_to_azure(local_file_path: Path, blob_name: str = None, max_retries: int = 3, file_backend_url: str = None) -> Tuple[bool, str, str]

    • Upload a file to Azure Blob Storage
    • Returns: (success, public_url, blob_url)
  • is_initialized() -> bool

    • Check if Azure is initialized
  • get_blob_service_client()

    • Get the BlobServiceClient
  • get_container_client()

    • Get the ContainerClient
  • get_container_name() -> str

    • Get the configured container name

GCP Module (os_gcp)

Functions

  • initialize_gcp(file_backend_url: str = None) -> bool

    • Initialize GCP client using service account JSON
    • Returns: True if successful, False otherwise
  • upload_to_gcp(local_file_path: Path, blob_name: str = None, max_retries: int = 3, file_backend_url: str = None) -> Tuple[bool, str, str]

    • Upload a file to GCP Cloud Storage
    • Returns: (success, public_url, gcs_url)
  • is_initialized() -> bool

    • Check if GCP is initialized
  • get_storage_client()

    • Get the GCP storage client
  • get_bucket()

    • Get the bucket object
  • get_bucket_name() -> str

    • Get the configured bucket name
  • get_project_id() -> str

    • Get the configured project ID

Features in Detail

Retry Logic

All providers include automatic retry with exponential backoff:

  • Default: 3 retry attempts
  • Configurable via max_retries parameter

CDN Verification (Azure)

Azure uploads include automatic CDN verification:

  • Checks if the uploaded blob is accessible via CDN
  • Up to 32 retry attempts with 2-second delays
  • Ensures the file is properly cached and accessible

Path Management

  • Azure $web containers: Uses flat paths
  • Azure/GCP regular containers: Adds date-based paths (YYYY/MM/DD)
  • S3: Preserves your specified key structure

Error Handling

All functions return: (success: bool, public_url: str, cloud_url: str)

Requirements

  • Python >= 3.8
  • boto3 >= 1.26.0
  • azure-storage-blob >= 12.19.0
  • google-cloud-storage >= 2.10.0
  • requests >= 2.28.0

License

BSD License

Author

Oleg Smirnov - @BestianCode

Changelog

1.0.2 (2025-12-19)

  • Fixed GCP Cloud Storage file prefix

1.0.1 (2025-12-18)

  • Added GCP Cloud Storage support
  • Unified interface for all three providers

1.0.0 (2025-10-20)

  • Initial release with S3 and Azure support

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

os_py_lib_cloud-1.0.2.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

os_py_lib_cloud-1.0.2-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file os_py_lib_cloud-1.0.2.tar.gz.

File metadata

  • Download URL: os_py_lib_cloud-1.0.2.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for os_py_lib_cloud-1.0.2.tar.gz
Algorithm Hash digest
SHA256 7bb90be3eb7e2e5d7ef5d1bc63c524569bf4787a1686d704a95fe35ee2a99061
MD5 70f33342ceab54cea6f3ace95073614f
BLAKE2b-256 32b36284193347e3540c84e01ce860b18d5bb87413a8ec885a7d3437034b09e7

See more details on using hashes here.

File details

Details for the file os_py_lib_cloud-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for os_py_lib_cloud-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c1b72eeda135eead889c3a0bf92917347dc102420761983f66a4741e529389aa
MD5 036784a2c57c8fdd2d5a9f4cadc70240
BLAKE2b-256 8f0029538c3707f0d7aebdac8d5ad451a2660ebd44df269bb2cd068d077002e2

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