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

git clone https://github.com/juancabrera-r/filedeletemanager.git
cd filedeletemanager
pip install -e .

From PyPI (when published)

pip install gcs-easy

Configuration

  1. Copy config_example.yml to config.yml:

    cp config_example.yml config.yml
    
  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")

# 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.0.tar.gz (14.6 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.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gcs_easy-0.1.0.tar.gz
  • Upload date:
  • Size: 14.6 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.0.tar.gz
Algorithm Hash digest
SHA256 c4fd6b89cdd18b04b856b086ada9187cda48764151c24cf78b934140a198e894
MD5 b64738fb541f05c2547f005be6cd0f1a
BLAKE2b-256 0ef4713042928dd6607d748bbb474ebb0d9b37cf573f69978bf5598a9c22fcbf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gcs_easy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7851cabd4d86ce34ecca1c817777749d4ed22e22e0a3d82191cfeb4d6ec1c5a6
MD5 47afeb4979e131ec8547595cbf799175
BLAKE2b-256 c4ac887c63e1908d775ccae807ef717a670ad297ae02d7c8be3155de0c291ba4

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