Skip to main content

Python library for uploading files to R2 and registering them with the r2index API

Project description

elaunira-r2index

Python library for uploading and downloading files to/from Cloudflare R2 with the r2index API.

Installation

pip install elaunira-r2index

Usage

Sync Client

from elaunira.r2index import R2IndexClient, R2Config

client = R2IndexClient(
    api_url="https://r2index.example.com",
    api_token="your-bearer-token",
    r2_config=R2Config(
        access_key_id="your-r2-access-key-id",
        secret_access_key="your-r2-secret-access-key",
        endpoint_url="https://your-account-id.r2.cloudflarestorage.com",
        bucket="your-bucket-name",
    ),
)

# Upload and register a file
record = client.upload_and_register(
    local_path="./myfile.zip",
    bucket="my-bucket",
    category="software",
    entity="myapp",
    remote_path="/releases/myapp",
    remote_filename="myapp.zip",
    remote_version="v1",
    tags=["release", "stable"],
)

# Download a file and record the download
# IP address is auto-detected, user agent defaults to "elaunira-r2index/0.1.0"
path, record = client.download_and_record(
    bucket="my-bucket",
    object_id="/releases/myapp/v1/myapp.zip",
    destination="./downloads/myfile.zip",
)

Async Client

from elaunira.r2index import AsyncR2IndexClient, R2Config

async with AsyncR2IndexClient(
    api_url="https://r2index.example.com",
    api_token="your-bearer-token",
    r2_config=R2Config(
        access_key_id="your-r2-access-key-id",
        secret_access_key="your-r2-secret-access-key",
        endpoint_url="https://your-account-id.r2.cloudflarestorage.com",
        bucket="your-bucket-name",
    ),
) as client:
    # Upload
    record = await client.upload_and_register(
        local_path="./myfile.zip",
        bucket="my-bucket",
        category="software",
        entity="myapp",
        remote_path="/releases/myapp",
        remote_filename="myapp.zip",
        remote_version="v1",
        tags=["release", "stable"],
    )

    # Download
    path, record = await client.download_and_record(
        bucket="my-bucket",
        object_id="/releases/myapp/v1/myapp.zip",
        destination="./downloads/myfile.zip",
    )

Transfer Configuration

Control multipart transfer settings with R2TransferConfig:

from elaunira.r2index import R2IndexClient, R2Config, R2TransferConfig

client = R2IndexClient(
    api_url="https://r2index.example.com",
    api_token="your-bearer-token",
    r2_config=R2Config(...),
)

# Custom transfer settings
transfer_config = R2TransferConfig(
    multipart_threshold=100 * 1024 * 1024,  # 100MB (default)
    multipart_chunksize=32 * 1024 * 1024,   # 32MB chunks
    max_concurrency=64,                      # 64 parallel threads
    use_threads=True,                        # Enable threading (default)
)

path, record = client.download_and_record(
    bucket="my-bucket",
    object_id="/data/files/v2/largefile.zip",
    destination="./downloads/largefile.zip",
    transfer_config=transfer_config,
)

Default max_concurrency is 2x the number of CPU cores (minimum 4).

Progress Tracking

def on_progress(bytes_transferred: int) -> None:
    print(f"Downloaded: {bytes_transferred / 1024 / 1024:.1f} MB")

path, record = client.download_and_record(
    bucket="my-bucket",
    object_id="/releases/myapp/v1/myapp.zip",
    destination="./downloads/myfile.zip",
    progress_callback=on_progress,
)

License

MIT

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

elaunira_r2index-0.2.0.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

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

elaunira_r2index-0.2.0-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

Details for the file elaunira_r2index-0.2.0.tar.gz.

File metadata

  • Download URL: elaunira_r2index-0.2.0.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for elaunira_r2index-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f802e53aee57f784739e4217dfd8fdbc20a6a83f667ebf4b9f71cbb3e133ee3e
MD5 abc1b5d5014b7bfd02d190747deeab89
BLAKE2b-256 55cc2af09af16567d0a8bd8788c6a3563537e88c69649f36bfd227f7bbb493b2

See more details on using hashes here.

File details

Details for the file elaunira_r2index-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for elaunira_r2index-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a5d53ac9c736577a6c839b7689f16f14539a9eb067392e3f495494649441f391
MD5 f2cfd4286235ffa95e187a5be5bce04d
BLAKE2b-256 1afcd861dbca74db9df1f9b6a7a5074c0babe2033973ac734103d8410cd62246

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