Serving files directly from Django
Project description
Welcome to django_fileresponse
Serve files directly from Django.
django_fileresponse
is a library that allows you to serve files directly from Django.
Features of django_fileresponse
django_fileresponse
provides the following features for developers:
- Use asyncio to serve files with high concurrency directly from Django.
- Uses aiofiles to asynchronously read from filesystem and aiobotocore to asynchronously read from s3 compatible object stores
Installing
django_fileresponse
is on PyPI so you can just run pip install django_fileresponse
.
Replace Default ASGIHandler
You have to replace Djangos ASGIHandler
, because it synchronously calls __next__
in for part in response which makes it impossible to await reading from a filesystem/object-store.
So you have to replace the default ASGIHandler
in asgi.py
.
So instead of building your application like this:
from django.core.asgi import get_asgi_application
application = get_asgi_application()
<IPython.core.display.Javascript object>
You have to import a modified ASGIHandler from fileresponse:
from fileresponse.asgi import get_asgi_application
application = get_asgi_application()
<IPython.core.display.Javascript object>
If you use a different mechanism to launch your application, you could also just import the modified AsyncFileASGIHandler
directly:
from fileresponse.handlers import AsyncFileASGIHandler
django.setup(set_prefix=False)
application = AsyncFileASGIHandler()
<IPython.core.display.Javascript object>
How to use Async Fileresponses in your Views
Add functions below to your views.py
Serving from Filesystem
from fileresponse.http import AiofileFileResponse as AiofileFileResponse
async def get_file(request, path):
file_path = Path(path)
return AiofileFileResponse(file_path)
<IPython.core.display.Javascript object>
Serve Files from an S3 Compatible Object Store
from fileresponse.http import AiobotocoreFileResponse
async def get_file(request, key):
bucket = settings.FILERESPONSE_S3_ACCESS_KEY_ID
return AiobotocoreFileResponse(bucket, key, chunk_size=4096)
<IPython.core.display.Javascript object>
Settings
Example Settings for an S3 Compatible Object Store
FILERESPONSE_S3_ACCESS_KEY_ID="minioadmin"
FILERESPONSE_S3_SECRET_ACCESS_KEY="minioadmin"
FILERESPONSE_S3_REGION="us-west-2"
FILERESPONSE_S3_STORAGE_BUCKET_NAME="fileresponse"
FILERESPONSE_S3_ENDPOINT_URL="http://localhost:9000"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_fileresponse-0.1.0.tar.gz
.
File metadata
- Download URL: django_fileresponse-0.1.0.tar.gz
- Upload date:
- Size: 12.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff77552e57600425e8fd9339405017ab5d08ce30d74639eb8506575da71d00f7 |
|
MD5 | fff16b210e3b7aaf7e50b1c6a96af4b3 |
|
BLAKE2b-256 | cbe1b9bf4051067280bab10f0b6ed1fdef8a57f718c0f7be23f85d38bbea56c2 |
File details
Details for the file django_fileresponse-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: django_fileresponse-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae27355f9a029b3ea931797623ae39001b42228a2b0150c66fb49811a4f3e393 |
|
MD5 | 79545ceebb88bb4df3f71aa742251e78 |
|
BLAKE2b-256 | 857d8f02f053ccbc2296703853163c2dbda7f549d4c896a6c880ad37dea35c57 |