tusd

repository·main·Indexed 26 days ago

https://github.com/tus/tusd

The official reference implementation of the tus resumable upload protocol. tusd provides a server for handling HTTP-based file uploads that can be paused and resumed, supporting storage backends including local disk, AWS S3, Google Cloud Storage (GCS), and Azure BlockBlob Storage. It features an extensible hook system supporting file-based scripts, HTTP, gRPC, and Go plugins to trigger events such as pre-create and post-finish.

Tokens
15.8K
Snippets
41
Records
109
Agent score
77%

What's inside tusd

  1. Overview of tusd

    main

    tusd is the official reference implementation of the tus resumable upload protocol. It allows for HTTP-based file uploads that can be interrupted and resumed without re-uploading previously sent data, making it resilient to network issues or server outages.

    Key Features

    • Large File Support: Handles arbitrarily large files.
    • Client Compatibility: Works with any tus-compatible client.
    • Single Binary: Distributed as a standalone binary that does not require a runtime.
    • Extensibility: Fully customizable via hooks (Scripts, HTTP, or gRPC) for tasks like:
      • Upload validation
      • User authentication and authorization
      • File post-processing
    • Storage Backends: Supports multiple storage options including:
      • Local disk
      • AWS S3
      • Azure Blob Storage
      • Google Cloud Storage
    • Go Integration: Can be embedded directly into Go applications.
  2. Understand tusd upload locks

    main

    tusd uses locks to prevent concurrent requests to the same upload resource, which protects against data loss and corruption. This is necessary because network interruptions can cause a client to attempt a resume (via HEAD or PATCH) before the server has realized the previous connection was broken and cleaned up the resources.

    Locks are acquired for all following request types:

    • POST
    • PATCH
    • DELETE
    • HEAD
    • GET

    Even HEAD requests require a lock to ensure the upload state is not being modified by another request during the fetch.

  3. Customize tusd behavior using hooks

    main

    tusd provides a hook system to establish communication between the server and your application. Hooks trigger user-defined actions during specific upload events, enabling features like logging, validation, authorization, and post-processing.

    There are four delivery methods for hooks:

    1. File hooks: tusd executes a provided executable file or script.
    2. HTTP hooks: tusd sends an HTTP POST request to a custom endpoint.
    3. gRPC hooks: tusd invokes a method on a remote gRPC endpoint.
    4. Plugin hooks: tusd loads a plugin from disk and invokes its methods.
  4. Configure HTTPS/TLS

    main

    To enable HTTPS directly in tusd, provide a certificate and a private key using the -tls-certificate and -tls-key flags. The certificate must include the full chain.

    Supported TLS modes via -tls-mode:

    • tls12 (default): TLSv1.3 + TLSv1.2 with standard cipher suites.
    • tls12-strong: TLSv1.2 with 256-bit AES ciphers only.
    • tls13: TLSv1.3 only.
  5. Run the gRPC hook server example with Python

    main

    This example demonstrates how to set up and run a gRPC hook server using Python to interact with tusd. Follow these steps to prepare the environment, build the necessary gRPC code, start the server, and connect tusd to it.

    1. Setup virtual environment and install dependencies

    Create a virtual environment and install the required Python packages:

    python3 -m venv venv
    source venv/bin/activate
    pip3 install -r requirements.txt

    2. Build gRPC code

    If the gRPC code is not already generated, use make to build hook_pb2.py:

    make -B hook_pb2.py

    3. Start the gRPC server

    Run the server script. By default, it listens on localhost:8000:

    python3 server.py

    4. Connect tusd to the gRPC server

    In a separate terminal, run tusd and use the -hooks-grpc flag to point it to your running gRPC server:

    tusd -hooks-grpc localhost:8000
    # Setup virtual environment and install dependencies
    python3 -m venv venv
    source venv/bin/activate
    pip3 install -r requirements.txt
    
    # Build gRPC code, if necessary
    make -B hook_pb2.py
    
    # Start gRPC server (listening at localhost:8000)
    python3 server.py
    
    # In a separate terminal you can now run tusd and point it to the gRPC server
    tusd -hooks-grpc localhost:8000
  6. Post-process files using the post-finish hook

    main

    The post-finish hook is invoked after an upload is completed and data is saved to the data store. Use this for tasks like moving files to permanent locations or starting encoding.

    Key Characteristics:

    • Non-blocking: It is invoked after tusd has already responded to the client. Long-running tasks will not block user interaction.
    • No Retries: tusd does not retry post-finish hooks if they fail. For volatile or long-running tasks (like video encoding), use an external task management system.
  7. Configure File Hooks

    main

    File hooks allow tusd to execute scripts or executables from a specific directory when an event is triggered.

    Setup:

    1. Enable file hooks by passing the -hooks-dir flag with the path to your hook directory.
    2. Name your executable files exactly after the event they should handle (e.g., pre-create).

    Platform Specifics:

    • UNIX: Hook files must not have an extension (e.g., use pre-create, not pre-create.sh). Use a shebang (e.g., #!/usr/bin/env python3) to specify the interpreter.
    • Windows: Hook files must have an extension (e.g., .bat or .exe).

    Execution Environment:

    • Environment Variables: TUS_ID, TUS_OFFSET, and TUS_SIZE are provided. Note that TUS_ID is an empty string during the pre-create hook.
    • Stdin: A JSON-encoded hook request is available on stdin.
    • Stdout: If the process exits with code 0, tusd parses stdout as a JSON-encoded hook response to customize the HTTP response or abort uploads.
    • Stderr: Redirected to tusd's stderr for logging.

    Error Handling:

    • A non-zero exit code is treated as an internal failure. For pre-create and pre-finish, tusd will respond to the client with a 500 Internal Server Error and stop processing.
    $ tusd -hooks-dir ./path/to/hooks/
  8. Compile tusd from source

    main

    You can compile tusd manually if you have Go installed. It is recommended to use one of the two latest major releases of Go. Clone the repository and build the binary using the following commands:

    git clone https://github.com/tus/tusd.git
    cd tusd
    
    go build -o tusd cmd/tusd/main.go
    # The binary is saved in ./tusd