Skip to main content
https://img.shields.io/travis/edoburu/django-private-storage/master.svg?branch=master https://img.shields.io/pypi/v/django-private-storage.svg https://img.shields.io/pypi/l/django-private-storage.svg https://img.shields.io/codecov/c/github/edoburu/django-private-storage/master.svg

django-private-storage

This module offers a private media file storage, so user uploads can be protected behind a login.

It uses the Django storage API’s internally, so all form rendering and admin integration work out of the box.

Installation

pip install django-private-storage

Configuration

Add to the settings:

INSTALLED_APPS += (
    'private_storage',
)

PRIVATE_STORAGE_ROOT = '/path/to/private-media/'
PRIVATE_STORAGE_AUTH_FUNCTION = 'private_storage.permissions.allow_staff'

Add to urls.py:

import private_storage.urls

urlpatterns += [
    url('^private-media/', include(private_storage.urls)),
]

Usage

In a Django model, add the PrivateFileField:

from django.db import models
from private_storage.fields import PrivateFileField

class MyModel(models.Model):
    title = models.CharField("Title", max_length=200)
    file = PrivateFileField("File")

The PrivateFileField also accepts the following kwargs:

  • upload_to: the optional subfolder in the PRIVATE_STORAGE_ROOT.

  • upload_subfolder: a function that defines the folder, it receives the current model instance.

  • content_types: allowed content types

  • max_file_size: maximum file size.

  • storage: the storage object to use, defaults to private_storage.storage.private_storage

Other topics

Storing files on Amazon S3

The PRIVATE_STORAGE_CLASS setting can be redefined to point to a different storage class. The default is private_storage.storage.files.PrivateFileSystemStorage, which uses a private media folder that PRIVATE_STORAGE_ROOT points to.

Define one of these settings instead:

PRIVATE_STORAGE_CLASS = 'private_storage.storage.s3boto3.PrivateS3BotoStorage'

AWS_PRIVATE_STORAGE_BUCKET_NAME = 'private-files'  # bucket name

This uses django-storages settings. Replace the prefix AWS_ with AWS_PRIVATE_. The following settings are reused when they don’t have an corresponding AWS_PRIVATE_... setting:

  • AWS_ACCESS_KEY_ID

  • AWS_SECRET_ACCESS_KEY

  • AWS_S3_URL_PROTOCOL

  • AWS_S3_REGION_NAME

  • AWS_IS_GZIPPED

All other settings should be explicitly defined with AWS_PRIVATE_... settings.

By default, all URLs in the admin return the direct S3 bucket URls, with the query parameter authentication enabled. When AWS_PRIVATE_QUERYSTRING_AUTH = False, all file downloads are proxied through our PrivateFileView URL. This behavior can be enabled explicitly using PRIVATE_STORAGE_S3_REVERSE_PROXY = True.

To have encryption either configure AWS_PRIVATE_S3_ENCRYPTION and AWS_PRIVATE_S3_SIGNATURE_VERSION or use:

PRIVATE_STORAGE_CLASS = 'private_storage.storage.s3boto3.PrivateEncryptedS3BotoStorage'

Make sure an encryption key is generated on Amazon.

Defining access rules

The PRIVATE_STORAGE_AUTH_FUNCTION defines which user may access the files. By default, this only includes superusers.

The following options are available out of the box:

  • private_storage.permissions.allow_authenticated

  • private_storage.permissions.allow_staff

  • private_storage.permissions.allow_superuser

You can create a custom function, and use that instead. The function receives a private_storate.models.PrivateFile object, which has the following fields:

  • request: the Django request.

  • storage: the storage engine used to retrieve the file.

  • relative_name: the file name in the storage.

  • full_path: the full file system path.

  • exists(): whether the file exists.

  • content_type: the HTTP content type.

  • parent_object: only set when PrivateStorageDetailView was used.

Retrieving files by object ID

To implement more object-based access permissions, create a custom view that provides the download.

from private_storage.views import PrivateStorageDetailView

class MyDocumentDownloadView(PrivateStorageDetailView):
    model = MyModel
    model_file_field = 'file'

    def get_queryset(self):
        # Make sure only certain objects can be accessed.
        return super().get_queryset().filter(...)

    def can_access_file(self, private_file):
        # When the object can be accessed, the file may be downloaded.
        # This overrides PRIVATE_STORAGE_AUTH_FUNCTION
        return True

Optimizing large file transfers

Sending large files can be inefficient in some configurations.

In the worst case scenario, the whole file needs to be read in chunks and passed as a whole through the WSGI buffers, OS kernel, webserver and proxy server. In effect, the complete file is copied several times through memory buffers.

There are more efficient ways to transfer files, such as the sendfile() system call on UNIX. Django uses such feature when the WSGI server provides wsgi.file_handler support.

In some situations, this effect is nullified, for example by by a local HTTP server sitting in front of the WSGI container. A typical case would be running Gunicorn behind an Nginx or Apache webserver.

For such situation, the native support of the webserver can be enabled with the following settings:

For apache

PRIVATE_STORAGE_SERVER = 'apache'

For Nginx

PRIVATE_STORAGE_SERVER = 'nginx'
PRIVATE_STORAGE_INTERNAL_URL = '/private-x-accel-redirect/'

Add the following location block in the server config:

location /private-x-accel-redirect/ {
  internal;
  alias   /path/to/private-media/;
}

Other webservers

The PRIVATE_STORAGE_SERVER may also point to a dotted Python class path. Implement a class with a static serve(private_file) method.

Using multiple storages

The PrivateFileField accepts a storage kwarg, hence you can initialize multiple private_storage.storage.PrivateStorage objects, each providing files from a different location and base_url.

For example:

from django.db import models
from private_storage.fields import PrivateFileField
from private_storage.storage.files import PrivateFileSystemStorage

my_storage = PrivateFileSystemStorage(
    location='/path/to/storage2/',
    base_url='/private-documents2/'
)

class MyModel(models.Model):
    file = PrivateFileField(storage=my_storage)

Then create a view to serve those files:

from private_storage.views import PrivateStorageView
from .models import my_storage

class MyStorageView(PrivateStorageView):
    storage = my_storage

    def can_access_file(self, private_file):
        # This overrides PRIVATE_STORAGE_AUTH_FUNCTION
        return self.request.is_superuser

And expose that URL:

urlpatterns += [
    url('^private-documents2/(?P<path>.*)$', views.MyStorageView.as_view()),
]

Contributing

This module is designed to be generic. In case there is anything you didn’t like about it, or think it’s not flexible enough, please let us know. We’d love to improve it!

Release files for django-private-storage 1.2.2

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-private-storage 1.2.2
File Size Uploaded
django-private-storage-1.2.2.tar.gz 18.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-private-storage 1.2.2
File Interpreter ABI Platform
django_private_storage-1.2.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 40.2 kB

Release files / django-private-storage-1.2.2.tar.gz

Download URL django-private-storage-1.2.2.tar.gz
Size 18.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b6f2091da18c077cef4b04d278a1c9f333e907b6425bac03afc87ea2290c3c1e
BLAKE2b-256 checksum
How to use checksums
cbf983a2bc50bd104038d7e6bf0d087b2a3e0da3ce091b56ae3de14a9dc5e337
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_private_storage-1.2.2-py2.py3-none-any.whl

Download URL django_private_storage-1.2.2-py2.py3-none-any.whl
Size 21.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
db7c434b1592abe0b73085dff018ec7bc52385d1989792efae9a815c1c6f9dd3
BLAKE2b-256 checksum
How to use checksums
c272c737e5c6b27289da4eb66dfc90a7ba682b3b4cf972bf1974a741228e55bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

3.1.3

2 release files

3.1.1

2 release files

3.1

2 release files

3.0

2 release files

2.3.0

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2

2 release files

2.1.3

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1

2 release files

2.0

2 release files

1.2.4

2 release files

1.2.3

2 release files

This release

1.2.2 This release

2 release files

1.2.1

2 release files

1.2

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0

1 release file

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