Tuspy Client for Tus Resumable Upload Protocol

1.1.0 · active · verified Sun Apr 12

tuspy is a Python client library for the tus resumable upload protocol (http://tus.io). It allows developers to reliably upload large files over HTTP, resuming interrupted uploads from where they left off. The current version is 1.1.0, and the library receives active maintenance with releases addressing bug fixes and minor feature enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `TusClient` and perform a synchronous file upload using the `Uploader`. It creates a dummy file if one doesn't exist and attempts to upload it to a specified tus server endpoint. Remember to set the `TUS_SERVER_ENDPOINT` environment variable or replace it directly.

import os
from tusclient import client

# Replace with your tus server endpoint
TUS_ENDPOINT = os.environ.get('TUS_SERVER_ENDPOINT', 'http://master.tus.io/files/')
FILE_TO_UPLOAD = 'path/to/your/local/file.txt' # Make sure this file exists

# Create a dummy file for demonstration if it doesn't exist
if not os.path.exists(FILE_TO_UPLOAD):
    with open(FILE_TO_UPLOAD, 'w') as f:
        f.write('This is a test file for tuspy upload.\n' * 100)
    print(f"Created dummy file: {FILE_TO_UPLOAD}")


try:
    # 1. Create a TusClient instance
    my_client = client.TusClient(TUS_ENDPOINT)

    # 2. Create an Uploader instance
    # For synchronous uploads, use my_client.uploader()
    # For asynchronous uploads, use my_client.async_uploader() and await upload()
    uploader = my_client.uploader(FILE_TO_UPLOAD, chunk_size=2 * 1024 * 1024) # 2MB chunks

    # 3. Start the upload
    print(f"Attempting to upload {FILE_TO_UPLOAD} to {TUS_ENDPOINT}...")
    uploader.upload()
    print(f"Upload of {FILE_TO_UPLOAD} complete. Upload URL: {uploader.url}")

except Exception as e:
    print(f"An error occurred during upload: {e}")
    print("Please ensure your TUS_SERVER_ENDPOINT is correct and accessible.")
finally:
    # Clean up dummy file
    if os.path.exists(FILE_TO_UPLOAD):
        os.remove(FILE_TO_UPLOAD)
        print(f"Cleaned up dummy file: {FILE_TO_UPLOAD}")

view raw JSON →