Skip to main content

Django Project to handle Static and Upload Files on s3 the right way

Project description

devpro-s3-storages

Django Project to handle Static and Upload Files on s3 the right way

How to use

Install it using your pacakage manager:

poetry add devpro-s3-storages

Configure your Django Settings to use those handlers:

AWS_STORAGE_BUCKET_NAME = '' # Empty means no s3 configuration so local filesystem will be used. Suitable for dev env 

if AWS_STORAGE_BUCKET_NAME == '':
    # Config to make static files locally
    STATIC_URL = 'static/'
    STATIC_ROOT = BASE_DIR / 'staticfiles'
    
    # Config to make uploading/downloading files locally
    MEDIA_ROOT = BASE_DIR / 'mediafiles'
    MEDIA_URL = '/mediafiles/'
    
    # Configuration to raise error in case ImageField/FileField does not define the property "upload_to" having 
    # "public/" or "private/" as prefix. See Model Usage to check those options
    STORAGES = {
        "default": {
            "BACKEND": "devpro_s3_storages.handlers.FileSystemWithValidationStorage",
        },
        "staticfiles": {
            "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
        },
    }
else:
    # Configuration to work using S3 bucket for static and file uploads/downloads
    STATIC_URL = f'//{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/static/'

    AWS_S3_ACCESS_KEY_ID = 'AWS_S3_ACCESS_KEY_ID'
    AWS_S3_SECRET_ACCESS_KEY = 'AWS_S3_SECRET_ACCESS_KEY'
    STORAGES = {
        "default": {
            "BACKEND": "devpro_s3_storages.handlers.S3FileStorage",
            "OPTIONS": {
                'default_acl': 'private',
                'location': 'media',
            },
        },
        "staticfiles": {
            "BACKEND": "storages.backends.s3.S3Storage",
            "OPTIONS": {
                'default_acl': 'public-read',
                'location': 'static',
                'querystring_auth': False
            },
        },
    }

So what this configuration will do when S3 bucket settings are provided?

Collect Static

Running python manage.py collectstatic will upload files to s3 with public-read permissions (Ensure to allow this on your S3 bucket configuration). It will upload your static files under "static" folder in your bucket It will generate unsigned urls for statics. Since static will be accessed on your site, they are already public, so why spending resource on creation signed urls for them?

File upload and download

When configuring ImageField/FileField in you models, you will need to define it the file will be public available or private. This will be setup using the "upload_to" property:

class PrivateFile(models.Model):
    private_file = models.FileField(upload_to='private/right')


class PublicFile(models.Model):
    public_file = models.FileField(upload_to='public/right')

So PrivateFile.private_file will be uploaded to bucket folder "media/private/right". It will have private access, meaning only signed url will be able to access this content. So PrivateFile.private_file.url() method will always generate a private signed url

On the other hand PublicFile.private_file will be uploaded to bucket folder "media/public/right". It will have public access, meaning ounsigned url will be able to access this content. So PublicFile.private_file.url() method will always generate a public url

Why this organization?

Some files are public, such as your avatar image in a social network. So it can better levarage cache and having a simple url.

Some file need privacy, such as some tax report you are collection. Si a signed url is needed for security puposes.

So this simple library delivery this kind of organization.

How to serve upload files locally?

The last step to having a proper local environment is making django to serv upload files when in dev environment. So add this config to the final of your urls.py file:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns=[]  # your mapped paths in here

# Add this path in case s3 bucket is not configured on settings
if settings.AWS_STORAGE_BUCKET_NAME == '':
    urlpatterns.extend(static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT))

You can check a project example on https://github.com/devpro-br/devpro-s3-storages/tree/main/django_project_ex

Cheers!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

devpro_s3_storages-0.2.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

devpro_s3_storages-0.2.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file devpro_s3_storages-0.2.0.tar.gz.

File metadata

  • Download URL: devpro_s3_storages-0.2.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/5.15.153.1-microsoft-standard-WSL2

File hashes

Hashes for devpro_s3_storages-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9a563887cb4ad4951134c48091dc6cdd89cbbccc30050a7353b3bb00ee544096
MD5 5154ad9701299f2db46af01cdfce13c6
BLAKE2b-256 776b222a8a02969bc30da110b37644e29c584839972118676464708a96e2ce5c

See more details on using hashes here.

File details

Details for the file devpro_s3_storages-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: devpro_s3_storages-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Linux/5.15.153.1-microsoft-standard-WSL2

File hashes

Hashes for devpro_s3_storages-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9c0efa30b40315cb08ecade9bb138c33160bcc540f0810d1f2b81cdc45a1c2dd
MD5 5c00d1373005d869b92c2e3295e2ec0f
BLAKE2b-256 540a0fcd9f26898f5a4af17eea7dc6502a20fc722427638a16754c295d04da51

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page