Skip to main content

Django application provide custom storage uses S3 and Compressor.

Project description

django-custom-storage PyPi license

PyPi status PyPi version PyPi python version PyPi downloads PyPi downloads PyPi downloads

GitHub GitHub release GitHub release

Test codecov.io pre-commit.ci status gitthub.com

Check Demo Project

  • Check the demo repo on GitHub

Requirements

  • Python 3.9+ supported.
  • Django 3.2+ supported.

Setup

  1. Install from pip:
pip install django-custom-storage
  1. Modify settings.py by adding the app to INSTALLED_APPS:
INSTALLED_APPS = [
    # ...
    "compressor",
    "custom_storage",
    # ...
]
  1. Finally, Modify settings.py by adding the needed configurations:
# ...other setting...
# Media files (Uploaded Images, Documents, Video, Audio)
MEDIA_ROOT = "/var/opt/your_project_name/mediaroot/"
# Static files (Fonts, CSS, JavaScript, Icons, Theme's Images)
STATIC_URL = "/static/"
STATIC_ROOT = "/var/cache/your_project_name/staticroot/"

# Example: cdn.your_project_bucket_name.com
AWS_STORAGE_BUCKET_PREFIX = "cdn"
AWS_STORAGE_BUCKET_NAME = "your_project_bucket_name"
AWS_STORAGE_BUCKET_TLD = "com"

# Access Key ID & Secret Access Key
AWS_ACCESS_KEY_ID = "YOUR_PROJECT_AWS_ACCESS_KEY_ID"
AWS_SECRET_ACCESS_KEY = "YOUR_PROJECT_AWS_SECRET_ACCESS_KEY"
# ...other setting...

# START - Compress and Upload on S3
import os
import sys
import datetime

# Add option to FORCE_LOCAL_STORAGE
# https://www.mslinn.com/django/1300-django-aws-control.html
if "--force-local-storage" in sys.argv:
    print("AWS datastore disabled; using local storage for assets instead.")
    FORCE_LOCAL_STORAGE = True
    sys.argv.remove("--force-local-storage")
else:
    print("Using AWS datastore for assets.")
    FORCE_LOCAL_STORAGE = False

if not FORCE_LOCAL_STORAGE:
    DEFAULT_FILE_STORAGE = "custom_storage.storage.MediaRootCachedS3Storage"
    if not os.getenv("RUN_COMPRESS", False):
        STATICFILES_STORAGE = "custom_storage.storage.StaticRootCachedS3Storage"
else:
    DEFAULT_FILE_STORAGE = "django.core.files.storage.FileSystemStorage"

COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True

STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
    "compressor.finders.CompressorFinder",
)
COMPRESS_ROOT = STATIC_ROOT

AWS_S3_CUSTOM_DOMAIN = (
    f"{AWS_STORAGE_BUCKET_PREFIX}.{AWS_STORAGE_BUCKET_NAME}.{AWS_STORAGE_BUCKET_TLD}"
)

STATIC_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/static/"
MEDIA_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/media/"

AWS_DEFAULT_ACL = "public-read"
AWS_QUERYSTRING_AUTH = False
AWS_FILE_EXPIRE = 200
AWS_PRELOAD_METADATA = True

two_months = datetime.timedelta(days=61)
date_two_months_later = datetime.date.today() + two_months
expires = date_two_months_later.strftime("%A, %d %B %Y 20:00:00 GMT")
AWS_S3_OBJECT_PARAMETERS = {
    "Expires": expires,
    "CacheControl": "max-age=%d" % (int(two_months.total_seconds()),),
}

COMPRESS_CSS_HASHING_METHOD = None
COMPRESS_CSS_FILTERS = [
    "compressor.filters.css_default.CssAbsoluteFilter",
    # 'compressor.filters.css_default.CssRelativeFilter',
    "compressor.filters.cssmin.CSSMinFilter",
]
COMPRESS_JS_FILTERS = [
    "compressor.filters.jsmin.JSMinFilter",
]

COMPRESS_OUTPUT_DIR = "compressed_static"
COMPRESS_STORAGE = "custom_storage.storage.StaticRootCachedS3Storage"
# END - Compress and Upload on S3

Run Example Project

git clone --depth=50 --branch=django-custom-storage https://github.com/DLRSP/example.git DLRSP/example
cd DLRSP/example
python manage.py runserver

Now browser the app @ http://127.0.0.1:8000

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

django_custom_storage-0.3.2.tar.gz (41.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_custom_storage-0.3.2-py3-none-any.whl (78.1 kB view details)

Uploaded Python 3

File details

Details for the file django_custom_storage-0.3.2.tar.gz.

File metadata

  • Download URL: django_custom_storage-0.3.2.tar.gz
  • Upload date:
  • Size: 41.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_custom_storage-0.3.2.tar.gz
Algorithm Hash digest
SHA256 7f5d5c4275fd8f688274526a08887e0b7373e8485b772b304cc351122c526287
MD5 a3586da9d00b7823fd7d3c3e939c12cb
BLAKE2b-256 2d3808c869f3b67e2296843cdcb5465c498f7df575865e4d43c75ae9edef215b

See more details on using hashes here.

File details

Details for the file django_custom_storage-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_custom_storage-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ee55874a75b66e488fe45a6267770e4b43f0e63b08dd70e64c2c56f2c8c2864d
MD5 e31236edf0e846c3ae32e8299cc297b7
BLAKE2b-256 104441e9a5854ea9f79e93602d2138cd72bafa0e3b04f32dc73e9b9c37ec30fd

See more details on using hashes here.

Supported by

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