Skip to main content

DJANGO FILE FIELD ENCRYPTED

django-filefield-encrypted - An encrypted file field for Django.

This will encrypt when saving and decrypt on reading files on the fly. Files will be stored in a directory other than MEDIA_ROOT, so not exposed through 'media/...'.

INSTALLATION

Install django-filefield-encrypted:

.. code:: shell

pip install django-filefield-encrypted

Then add to your settings.py:

.. code:: python

# the key to encrypt the files. Keep it safe!
ENCRYPTED_FILES_KEY = b"<your key here>"
# The directory where files will be stored.
SAFE_MEDIA_ROOT = BASE_DIR / "safe/"

You can generate a key with:

.. code:: python

from cryptography.fernet import Fernet
key = Fernet.generate_key()
print(key)

USAGE

In your models.py:

.. code:: python

    from encrypted_files.fields import EncryptedFileField

    class Foobar(models.Model):
        foo = EncryptedFileField(upload_to="whatever/")

You can create your records normally. To read the file contents:

.. code:: python

    from encrypted_files.fields import EncryptedFileField

    obj = Foobar.objects.get(pk=pk)
    with obj.foo.open(mode="rb") as f:
        content = f.read()

To retrieve the contents of the file in a view:

.. code:: python

    from encrypted_files.views import EncryptedFileDetailView

    class FoobarView(EncryptedFileDetailView):
        model = Foobar
        encrypted_file_field = "foo"

Add this view to your urls.py. When you go to that url it will return the plain contents of the file. Treat if like a ´django.views.generic.detail.DetailView´. All you need is to indicate the model and the ´encrypted_file_field´ which is the field that contains the encrypted file. So let's say you want to store an image, and use it in your html. Given the above example. This is how the template would look like:

.. code:: html

    <img src="{% url 'foo_view_url_name' object.pk %}" />

That's it! The rendering page will load the image from that url, that will return the contents of the encrypted file transparently.

Alternatively you can also use it to download the file, in your template add the download link:

.. code:: html

    <a href="{% url 'foo_view_url_name' object.pk %}" download>
        click here...
    </a>

Metadata

Release files for django-filefield-encrypted 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-filefield-encrypted 0.1.1
File Size Uploaded
django-filefield-encrypted-0.1.1.tar.gz 16.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-filefield-encrypted 0.1.1
File Interpreter ABI Platform
django_filefield_encrypted-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 35.6 kB

Release files / django-filefield-encrypted-0.1.1.tar.gz

Download URL django-filefield-encrypted-0.1.1.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
cbe0871178fa5c330ad9598666941bc56abb78ebe4556633b2519fe8e5083831
BLAKE2b-256 checksum
How to use checksums
dcaa303abeee7b12b2d0f44417029d37dfed7258a92981bb494a2841ffd4f2a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.2

Release files / django_filefield_encrypted-0.1.1-py3-none-any.whl

Download URL django_filefield_encrypted-0.1.1-py3-none-any.whl
Size 18.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
db13177ff0593c5f539e641618aa869419405279945261e953a316fd265b47b5
BLAKE2b-256 checksum
How to use checksums
f53267f58017a75c2b8d0cfbc7a28cc3bbd6e2325043a6c54ba13c9279fe4aba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.2

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.0.9

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page