Skip to main content

Simple functions for working with S3

Project description

s3func

Simple functions for working with S3 and B2

build codecov PyPI version


s3func is a lightweight Python library providing a simplified interface for interacting with S3-compatible object storage services and Backblaze B2. It removes the boto3 dependency in favor of a fast, urllib3-based client with custom SigV4 signing.

Key Features

  • Zero Boto3 Dependency: Minimal overhead and faster imports.
  • S3 & B2 Support: Unified interface for both AWS S3 (and compatible) and Backblaze B2 native API.
  • Distributed Locking: Robust shared and exclusive locking mechanism using object storage.
  • Streaming Support: Efficiently stream large objects.
  • Automatic Retries: Built-in adaptive retry logic for transient network issues.

Installation

pip install s3func

Usage Examples

S3 Operations

from s3func import S3Session

# Initialize session for AWS S3
session = S3Session(
    access_key_id='YOUR_ACCESS_KEY',
    access_key='YOUR_SECRET_KEY',
    bucket='my-bucket',
    region='us-east-1'
)

# Also works with other S3-compatible providers (Contabo, Wasabi, DigitalOcean, etc.)
# by providing an endpoint_url.
session = S3Session(
    access_key_id='YOUR_ACCESS_KEY',
    access_key='YOUR_SECRET_KEY',
    bucket='my-bucket',
    endpoint_url='https://eu2.contabostorage.com'
)

# Upload an object
session.put_object('hello.txt', b'Hello, S3!')

# Download an object
resp = session.get_object('hello.txt')
print(resp.data.decode())

# List objects
for obj in session.list_objects(prefix='logs/').iter_objects():
    print(obj['key'], obj['content_length'])

Custom Metadata

You can easily read and write custom metadata headers.

# Upload with metadata
session.put_object(
    'data.csv', 
    b'col1,col2\n1,2', 
    metadata={'processed': 'false', 'source': 'sensor-1'}
)

# Read metadata
resp = session.head_object('data.csv')
print(resp.metadata['processed']) # 'false'

Backblaze B2 Operations

s3func uses the B2 native API for better performance and reliability on Backblaze.

from s3func import B2Session

# Initialize B2 session
session = B2Session(
    access_key_id='YOUR_APPLICATION_KEY_ID',
    access_key='YOUR_APPLICATION_KEY',
    bucket='my-b2-bucket'
)

# Put object
session.put_object('data.json', b'{"status": "ok"}', content_type='application/json')

Distributed Locking

s3func provides a powerful distributed lock that mimics Python's threading.Lock API.

# Using S3 Lock via context manager
with session.lock('process-1'):
    # This block is protected by a distributed lock
    print("Doing some exclusive work...")

# Explicit acquire/release with timeout
lock = session.lock('my-resource')
if lock.acquire(blocking=True, timeout=10):
    try:
        # Perform operation
        pass
    finally:
        lock.release()

Performance Tips

  • Streaming: Set stream=True in the session (default) or individual requests to handle large files without loading them entirely into memory.
  • B2 Authorization Caching: s3func automatically caches B2 authorization tokens globally for up to 1 hour, significantly reducing latency for concurrent operations.
  • Adaptive Retries: The library uses urllib3 retry logic configured for high-concurrency environments to handle rate limiting and network blips automatically.

How Distributed Locking Works

The locking mechanism uses a "two-object sequential" protocol to ensure safety even in eventually consistent systems:

  1. Acquisition: A worker writes two small objects (seq-0 and seq-1) to the storage provider.
  2. Verification: It then lists all existing lock objects for that resource.
  3. Conflict Resolution: A worker "wins" the lock if its seq-1 timestamp is the earliest among all candidates. If timestamps are identical, the lexicographical order of a unique lock_id acts as a tie-breaker.
  4. Auto-Cleanup: Uses weakref.finalize to ensure lock objects are deleted even if the process exits unexpectedly (best effort).

Development

Setup environment

We use uv to manage the development environment and production build.

uv sync --all-extras --dev

Running Tests

uv run pytest

License

This project is licensed under the terms of the Apache Software License 2.0.

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

s3func-0.8.3.tar.gz (27.2 kB view details)

Uploaded Source

Built Distribution

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

s3func-0.8.3-py3-none-any.whl (33.1 kB view details)

Uploaded Python 3

File details

Details for the file s3func-0.8.3.tar.gz.

File metadata

  • Download URL: s3func-0.8.3.tar.gz
  • Upload date:
  • Size: 27.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.7

File hashes

Hashes for s3func-0.8.3.tar.gz
Algorithm Hash digest
SHA256 df9d35edfdb2c99341033eafa40020c6014f18f0d3d28d9a87c6a475c008f0a5
MD5 bdac3e6ecf3cbc8419a1e257a4e3e4f8
BLAKE2b-256 a533202ee37a9bf368123ba37dd861a70fe199db5ddebd3ae4342615093cc549

See more details on using hashes here.

File details

Details for the file s3func-0.8.3-py3-none-any.whl.

File metadata

  • Download URL: s3func-0.8.3-py3-none-any.whl
  • Upload date:
  • Size: 33.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.7

File hashes

Hashes for s3func-0.8.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a6ad10c9876f36fe3b1006a91197d79699d8bc611e35eb0d0514a1844580c16e
MD5 ceef292cb7a75dbb32ac924ecbbc9bda
BLAKE2b-256 4794060e77a04e649f4943ade607f05004888f8deba6bea7653936567f5eb062

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