Tuspy Client for Tus Resumable Upload Protocol
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
- breaking Python 2 support was officially dropped in version 1.0.0. Projects still relying on Python 2 must use tuspy versions prior to 1.0.0.
- gotcha When working with `asyncio`, ensure you use `AsyncUploader` provided by `tusclient.asyncclient` and instantiate it via `client.async_uploader()`. The default `Uploader` is for synchronous operations.
- gotcha Incorrect `chunk_size` can lead to performance issues or errors if the server has strict limits. Larger files benefit from larger `chunk_size` but must not exceed server-defined limits.
- gotcha Ensure the tus server endpoint (e.g., `http://master.tus.io/files/`) is correct and accessible. A common mistake is providing a file URL instead of the base tus endpoint for uploads.
Install
-
pip install tuspy
Imports
- TusClient
from tusclient import client my_client = client.TusClient(...)
- Uploader
from tusclient.uploader import Uploader
- AsyncUploader
from tusclient.asyncclient import AsyncUploader
Quickstart
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}")