/django-zip-stream

Django extension to assemble ZIP archives dynamically using Nginx.

Primary LanguagePythonMIT LicenseMIT

django-zip-stream

Build Status codecov Code Health Codacy Badge license

Django extension to assemble ZIP archives dynamically using Nginx with mod\_zip.

ZIP archive generation alternatives such as ZipStream can tie up web server threads and make Python do the heavy lifting. To achieve higher performance, django-zip-stream offloads ZIP archive generation to Nginx/mod_zip which frees up web servers to serve other clients.

To use this library, setup Nginx (with mod_zip installed) as a reverse proxy for your Python web app.

Requirements

  • Django 1.7 +
  • Python 2.7, 3.4, 3.5, or pypy
  • Nginx 0.7.25 or later compiled with mod\_zip

See the Travis CI build matrix for detailed information regarding the latest master.

Installation

pip install git+https://github.com/travcunn/django-zip-stream.git

Examples

Django view that streams a zip with 2 files

from django_zip_stream.responses import TransferZipResponse

def download_zip(request):
    # Files are located at /home/travis but Nginx is configured to serve from /data
    files = [
       ("/chicago.jpg", "/data/home/travis/chicago.jpg", 4096),
       ("/portland.jpg", "/data//home/travis/portland.jpg", 4096),
    ]
    return TransferZipResponse(filename='download.zip', files=files)

Sample reverse proxy Nginx configuration

# Compile Nginx with mod_zip - https://github.com/evanmiller/mod_zip

server {
    listen 80;

    # Replace the following with the IP/port of your Python web application
    location / {
        proxy_pass http://192.168.12.41:8000;
    }

    # Configure nginx to serve files with absolute paths from an internal location
    # mod_zip location helper
    # Note: The leading ^~ is essential here, no more checks should be done after this match
    location ^~ /data/ {
        root /;
        internal;
    }
}

Resources