Skip to main content

Autenticación sencilla y utilidades para Google Cloud Storage (subir, bajar, firmar URLs).

Project description

GCS Easy

A simple and easy-to-use Python library for Google Cloud Storage operations with CLI support.

Features

  • 🔐 Simple authentication using service account credentials
  • 📤 Upload files with resumable uploads and progress tracking
  • 📥 Download files from GCS
  • 📋 List bucket contents
  • 🗑️ Delete files
  • 🔗 Generate signed URLs for secure access
  • ⚙️ Flexible configuration via YAML
  • 🖥️ Command-line interface for quick operations
  • 🔍 Built-in permissions checker

Installation

From Source

From PyPI (when published)

pip install gcs-easy

Configuration

  1. Add to config.yml:

     # Google Cloud Storage Configuration
     # Add your GCP credentials file path here
     GOOGLE_APPLICATION_CREDENTIALS : /path/to/your/credentials.json
    
     # GCS Client Configuration
     # default_bucket can be just bucket name (e.g., "your-bucket-name")
     # or bucket with default path/prefix (e.g., "your-bucket-name/your-path")
     default_bucket: your-bucket-name
     location: EU
     uniform_access: true
     chunk_size: 8388608  # 8MB in bytes
     cache_control: "public, max-age=3600"
     signed_url_expires_minutes: 15
    
  2. Edit config.yml with your settings:

    • Set GOOGLE_APPLICATION_CREDENTIALS to the path of your service account JSON file
    • Configure default_bucket (bucket name or bucket/path)
    • Adjust other settings as needed

Configuration Parameters

  • GOOGLE_APPLICATION_CREDENTIALS: Path to your Google Cloud service account JSON file
  • default_bucket: Default GCS bucket name (can include a path prefix)
  • location: GCS location for bucket creation (default: "EU")
  • uniform_access: Enable uniform bucket-level access (default: true)
  • chunk_size: Upload chunk size in bytes (default: 8388608)
  • cache_control: Cache control header for uploaded files
  • signed_url_expires_minutes: Default expiration time for signed URLs (default: 15)

Usage

Command Line Interface

The gcs-easy command provides quick access to GCS operations:

# Upload a file
gcs-easy upload --bucket your-bucket --src /path/to/file.txt --dst remote/path/file.txt

# Make file public during upload
gcs-easy upload --bucket your-bucket --src /path/to/file.txt --dst remote/path/file.txt --public

# Download a file
gcs-easy download --bucket your-bucket --src remote/path/file.txt --dst /local/path/file.txt

# List bucket contents
gcs-easy list --bucket your-bucket --prefix folder/

# Generate signed URL
gcs-easy sign --bucket your-bucket --path remote/path/file.txt --minutes 60

# Check permissions and configuration
gcs-easy permissions

Programmatic Usage

from gcs_easy import GCSClient

# Initialize client (uses config.yml settings)
client = GCSClient()

# Or specify bucket explicitly
client = GCSClient(default_bucket="your-bucket")

# Or specify custom config file path
client = GCSClient(config_path="/path/to/custom/config.yml")

# Upload a file
result = client.upload_file(
    local_path="/path/to/file.txt",
    blob_path="remote/path/file.txt",
    make_public=False
)
print(f"Uploaded {result.size} bytes to {result.bucket}/{result.blob}")

# Download a file
local_file = client.download_file(
    blob_path="remote/path/file.txt",
    local_path="/local/path/file.txt"
)

# List files
for blob_name in client.list(prefix="folder/"):
    print(blob_name)

# Check if file exists
if client.exists("remote/path/file.txt"):
    print("File exists")

# Delete a file
client.delete("remote/path/file.txt")

# Generate signed URL
url = client.signed_url("remote/path/file.txt", expires_minutes=30)
print(f"Signed URL: {url}")

Authentication

This library uses Google Cloud service account authentication. You need:

  1. A Google Cloud service account with appropriate GCS permissions
  2. The service account JSON key file
  3. The path to this file in your config.yml

Required Permissions

The service account needs these GCS permissions:

  • storage.objects.create - Upload files
  • storage.objects.get - Download files and generate signed URLs
  • storage.objects.list - List bucket contents
  • storage.objects.delete - Delete files
  • storage.buckets.get - Check bucket existence

Checking Permissions

Use the built-in permissions checker:

gcs-easy permissions

This will analyze your configuration and show required permissions.

Examples

Basic Upload/Download

from gcs_easy import GCSClient

client = GCSClient()

# Upload
client.upload_file("local_file.txt", "uploads/file.txt")

# Download
client.download_file("uploads/file.txt", "downloaded_file.txt")

Working with Different Buckets

# Use default bucket from config
client = GCSClient()

# Specify bucket for this operation
client.upload_file("file.txt", "path/file.txt", bucket_name="other-bucket")

Batch Operations

import glob
from pathlib import Path

client = GCSClient()

# Upload all .txt files in a directory
for file_path in Path("data/").glob("*.txt"):
    blob_path = f"data/{file_path.name}"
    client.upload_file(str(file_path), blob_path)
    print(f"Uploaded {file_path.name}")

Development

Setup Development Environment

# Create virtual environment
python -m venv .venv

# Activate (Windows)
.venv\Scripts\activate

# Activate (Linux/Mac)
source .venv/bin/activate

# Install in development mode
pip install -e ".[test]"

Running Tests

# Run all tests
pytest

# Run specific test file
pytest test/test_smoke.py

Building

# Build distribution
python -m build

# Upload to PyPI
python -m twine upload dist/*

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

License

MIT License - see LICENSE file for details.

Author

Juan Manuel Cabrera - juanmanuelcabrera.r@gmail.com

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

gcs_easy-0.1.2.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

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

gcs_easy-0.1.2-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file gcs_easy-0.1.2.tar.gz.

File metadata

  • Download URL: gcs_easy-0.1.2.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for gcs_easy-0.1.2.tar.gz
Algorithm Hash digest
SHA256 78493e27c8f93e7b539c885ea350db8867c42f52880bbc9c23e9a198bc9e0b09
MD5 2f29d11afa97d8000258b4f62b4093f5
BLAKE2b-256 c1df5797f34e7a42420253a25d9aba967f72d2436b539b3f12f4b728a2cacdcb

See more details on using hashes here.

File details

Details for the file gcs_easy-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: gcs_easy-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for gcs_easy-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ee6966dd85d85a3ad1015e2c7f1c93295d7845236a1b2bcd3de46e0455b7d7b8
MD5 b0f1c04e10742928be01e0d49fa6088d
BLAKE2b-256 39f77af6d31f573c392cccecda020ac4f2906dcd425df1f3c517bb1626f1fb72

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