travcunn / django-zip-stream

Django extension to assemble ZIP archives dynamically using Nginx.
MIT License
13 stars 2 forks source link

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

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 '''''''''''''''''''''''''''''''''''''''''''

.. code:: python

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

.. _mod_zip: https://github.com/evanmiller/mod_zip .. _ZipStream: https://github.com/SpiderOak/ZipStream

.. |Build Status| image:: https://travis-ci.org/travcunn/django-zip-stream.svg?branch=master :target: https://travis-ci.org/travcunn/django-zip-stream .. |codecov| image:: https://codecov.io/gh/travcunn/django-zip-stream/branch/master/graph/badge.svg :target: https://codecov.io/gh/travcunn/django-zip-stream .. |Code Health| image:: https://landscape.io/github/travcunn/django-zip-stream/master/landscape.svg?style=flat :target: https://landscape.io/github/travcunn/django-zip-stream/master .. |Codacy Badge| image:: https://api.codacy.com/project/badge/Grade/be7b93a01ebb4fb39aa3cbdfdabfccd9 :target: https://www.codacy.com/app/tcunningham/django-zip-stream .. |license| image:: https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000 :target: http://pypi.python.org/pypi/django-zip-stream