django-custom-storage


Namedjango-custom-storage JSON
Version 0.3.0 PyPI version JSON
download
home_pagehttps://github.com/DLRSP/django-custom-storage
SummaryDjango application provide custom storage uses S3 and Compressor.
upload_time2023-12-23 22:18:55
maintainer
docs_urlNone
authorDLRSP
requires_python>=3.8
licenseMIT License
keywords django s3 compressor
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # django-custom-storage [![PyPi license](https://img.shields.io/pypi/l/django-custom-storage.svg)](https://pypi.python.org/pypi/django-custom-storage)

[![PyPi status](https://img.shields.io/pypi/status/django-custom-storage.svg)](https://pypi.python.org/pypi/django-custom-storage)
[![PyPi version](https://img.shields.io/pypi/v/django-custom-storage.svg)](https://pypi.python.org/pypi/django-custom-storage)
[![PyPi python version](https://img.shields.io/pypi/pyversions/django-custom-storage.svg)](https://pypi.python.org/pypi/django-custom-storage)
[![PyPi downloads](https://img.shields.io/pypi/dm/django-custom-storage.svg)](https://pypi.python.org/pypi/django-custom-storage)
[![PyPi downloads](https://img.shields.io/pypi/dw/django-custom-storage.svg)](https://pypi.python.org/pypi/django-custom-storage)
[![PyPi downloads](https://img.shields.io/pypi/dd/django-custom-storage.svg)](https://pypi.python.org/pypi/django-custom-storage)

## GitHub ![GitHub release](https://img.shields.io/github/tag/DLRSP/django-custom-storage.svg) ![GitHub release](https://img.shields.io/github/release/DLRSP/django-custom-storage.svg)

## Test [![codecov.io](https://codecov.io/github/DLRSP/django-custom-storage/coverage.svg?branch=main)](https://codecov.io/github/DLRSP/django-custom-storage?branch=main) [![pre-commit.ci status](https://results.pre-commit.ci/badge/github/DLRSP/django-custom-storage/main.svg)](https://results.pre-commit.ci/latest/github/DLRSP/django-custom-storage/main) [![gitthub.com](https://github.com/DLRSP/django-custom-storage/actions/workflows/ci.yaml/badge.svg)](https://github.com/DLRSP/django-custom-storage/actions/workflows/ci.yaml)

## Check Demo Project
* Check the demo repo on [GitHub](https://github.com/DLRSP/example/tree/django-custom-storage)

## Requirements
-   Python 3.8+ supported.
-   Django 3.2+ supported.

## Setup
1. Install from **pip**:
```shell
pip install django-custom-storage
```

2. Modify `settings.py` by adding the app to `INSTALLED_APPS`:
```python
INSTALLED_APPS = [
    # ...
    "compressor",
    "custom_storage",
    # ...
]
```

3. Finally, Modify `settings.py` by adding the needed configurations:
```python
# ...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

```shell
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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/DLRSP/django-custom-storage",
    "name": "django-custom-storage",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "django, S3, compressor",
    "author": "DLRSP",
    "author_email": "dlrsp.dev@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/95/a6/aed1168f2b0477f84c500d957ceddaaa848cd24bd218c66dbde5dfc5ea95/django-custom-storage-0.3.0.tar.gz",
    "platform": null,
    "description": "# django-custom-storage [![PyPi license](https://img.shields.io/pypi/l/django-custom-storage.svg)](https://pypi.python.org/pypi/django-custom-storage)\n\n[![PyPi status](https://img.shields.io/pypi/status/django-custom-storage.svg)](https://pypi.python.org/pypi/django-custom-storage)\n[![PyPi version](https://img.shields.io/pypi/v/django-custom-storage.svg)](https://pypi.python.org/pypi/django-custom-storage)\n[![PyPi python version](https://img.shields.io/pypi/pyversions/django-custom-storage.svg)](https://pypi.python.org/pypi/django-custom-storage)\n[![PyPi downloads](https://img.shields.io/pypi/dm/django-custom-storage.svg)](https://pypi.python.org/pypi/django-custom-storage)\n[![PyPi downloads](https://img.shields.io/pypi/dw/django-custom-storage.svg)](https://pypi.python.org/pypi/django-custom-storage)\n[![PyPi downloads](https://img.shields.io/pypi/dd/django-custom-storage.svg)](https://pypi.python.org/pypi/django-custom-storage)\n\n## GitHub ![GitHub release](https://img.shields.io/github/tag/DLRSP/django-custom-storage.svg) ![GitHub release](https://img.shields.io/github/release/DLRSP/django-custom-storage.svg)\n\n## Test [![codecov.io](https://codecov.io/github/DLRSP/django-custom-storage/coverage.svg?branch=main)](https://codecov.io/github/DLRSP/django-custom-storage?branch=main) [![pre-commit.ci status](https://results.pre-commit.ci/badge/github/DLRSP/django-custom-storage/main.svg)](https://results.pre-commit.ci/latest/github/DLRSP/django-custom-storage/main) [![gitthub.com](https://github.com/DLRSP/django-custom-storage/actions/workflows/ci.yaml/badge.svg)](https://github.com/DLRSP/django-custom-storage/actions/workflows/ci.yaml)\n\n## Check Demo Project\n* Check the demo repo on [GitHub](https://github.com/DLRSP/example/tree/django-custom-storage)\n\n## Requirements\n-   Python 3.8+ supported.\n-   Django 3.2+ supported.\n\n## Setup\n1. Install from **pip**:\n```shell\npip install django-custom-storage\n```\n\n2. Modify `settings.py` by adding the app to `INSTALLED_APPS`:\n```python\nINSTALLED_APPS = [\n    # ...\n    \"compressor\",\n    \"custom_storage\",\n    # ...\n]\n```\n\n3. Finally, Modify `settings.py` by adding the needed configurations:\n```python\n# ...other setting...\n# Media files (Uploaded Images, Documents, Video, Audio)\nMEDIA_ROOT = '/var/opt/your_project_name/mediaroot/'\n# Static files (Fonts, CSS, JavaScript, Icons, Theme's Images)\nSTATIC_URL = '/static/'\nSTATIC_ROOT = '/var/cache/your_project_name/staticroot/'\n\n# Example: cdn.your_project_bucket_name.com\nAWS_STORAGE_BUCKET_PREFIX = \"cdn\"\nAWS_STORAGE_BUCKET_NAME = \"your_project_bucket_name\"\nAWS_STORAGE_BUCKET_TLD = \"com\"\n\n# Access Key ID & Secret Access Key\nAWS_ACCESS_KEY_ID = 'YOUR_PROJECT_AWS_ACCESS_KEY_ID'\nAWS_SECRET_ACCESS_KEY = 'YOUR_PROJECT_AWS_SECRET_ACCESS_KEY'\n# ...other setting...\n\n# START - Compress and Upload on S3\nimport os\nimport sys\nimport datetime\n\n# Add option to FORCE_LOCAL_STORAGE\n# https://www.mslinn.com/django/1300-django-aws-control.html\nif \"--force-local-storage\" in sys.argv:\n    print(\"AWS datastore disabled; using local storage for assets instead.\")\n    FORCE_LOCAL_STORAGE = True\n    sys.argv.remove(\"--force-local-storage\")\nelse:\n    print(\"Using AWS datastore for assets.\")\n    FORCE_LOCAL_STORAGE = False\n\nif not FORCE_LOCAL_STORAGE:\n    DEFAULT_FILE_STORAGE = 'custom_storage.storage.MediaRootCachedS3Storage'\n    if not os.getenv('RUN_COMPRESS', False):\n        STATICFILES_STORAGE = 'custom_storage.storage.StaticRootCachedS3Storage'\nelse:\n    DEFAULT_FILE_STORAGE = 'django.core.files.storage.FileSystemStorage'\n    \nCOMPRESS_ENABLED = True\nCOMPRESS_OFFLINE = True\n\nSTATICFILES_FINDERS = (\n    'django.contrib.staticfiles.finders.FileSystemFinder',\n    'django.contrib.staticfiles.finders.AppDirectoriesFinder',\n    'compressor.finders.CompressorFinder',\n)\nCOMPRESS_ROOT = STATIC_ROOT\n\nAWS_S3_CUSTOM_DOMAIN = f\"{AWS_STORAGE_BUCKET_PREFIX}.{AWS_STORAGE_BUCKET_NAME}.{AWS_STORAGE_BUCKET_TLD}\"\n\nSTATIC_URL = f\"https://{AWS_S3_CUSTOM_DOMAIN}/static/\"\nMEDIA_URL = f\"https://{AWS_S3_CUSTOM_DOMAIN}/media/\"\n\nAWS_DEFAULT_ACL = 'public-read'\nAWS_QUERYSTRING_AUTH = False\nAWS_FILE_EXPIRE = 200\nAWS_PRELOAD_METADATA = True\n\ntwo_months = datetime.timedelta(days=61)\ndate_two_months_later = datetime.date.today() + two_months\nexpires = date_two_months_later.strftime(\"%A, %d %B %Y 20:00:00 GMT\")\nAWS_S3_OBJECT_PARAMETERS = {\n    'Expires': expires,\n    'CacheControl': 'max-age=%d' % (int(two_months.total_seconds()),),\n}\n\nCOMPRESS_CSS_HASHING_METHOD = None\nCOMPRESS_CSS_FILTERS = [\n    'compressor.filters.css_default.CssAbsoluteFilter',\n    # 'compressor.filters.css_default.CssRelativeFilter',\n    'compressor.filters.cssmin.CSSMinFilter'\n]\nCOMPRESS_JS_FILTERS = [\n    'compressor.filters.jsmin.JSMinFilter',\n]\n\nCOMPRESS_OUTPUT_DIR = 'compressed_static'\nCOMPRESS_STORAGE = 'custom_storage.storage.StaticRootCachedS3Storage'\n# END - Compress and Upload on S3\n\n```\n\n\n## Run Example Project\n\n```shell\ngit clone --depth=50 --branch=django-custom-storage https://github.com/DLRSP/example.git DLRSP/example\ncd DLRSP/example\npython manage.py runserver\n```\n\nNow browser the app @ http://127.0.0.1:8000\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Django application provide custom storage uses S3 and Compressor.",
    "version": "0.3.0",
    "project_urls": {
        "Homepage": "https://github.com/DLRSP/django-custom-storage"
    },
    "split_keywords": [
        "django",
        " s3",
        " compressor"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "72e22401f471f9e0354795a6551417a964b90133fe32de58dc2cac9ababb0e59",
                "md5": "27b9186bdcdbf5978cd2a1d2d8c9394b",
                "sha256": "8cad2a93281fe8461a1b9098a31583074109c849dbbaa5d95f29726cf6455097"
            },
            "downloads": -1,
            "filename": "django_custom_storage-0.3.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "27b9186bdcdbf5978cd2a1d2d8c9394b",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": ">=3.8",
            "size": 5244,
            "upload_time": "2023-12-23T22:18:54",
            "upload_time_iso_8601": "2023-12-23T22:18:54.405273Z",
            "url": "https://files.pythonhosted.org/packages/72/e2/2401f471f9e0354795a6551417a964b90133fe32de58dc2cac9ababb0e59/django_custom_storage-0.3.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "95a6aed1168f2b0477f84c500d957ceddaaa848cd24bd218c66dbde5dfc5ea95",
                "md5": "01cb1e7b444596ec3b18fdfe18ea4593",
                "sha256": "cea5667d84786b6304a24f7e9ba09ab5fda5dc610f36dd5aaf2275e042d0f2e6"
            },
            "downloads": -1,
            "filename": "django-custom-storage-0.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "01cb1e7b444596ec3b18fdfe18ea4593",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 38322,
            "upload_time": "2023-12-23T22:18:55",
            "upload_time_iso_8601": "2023-12-23T22:18:55.922588Z",
            "url": "https://files.pythonhosted.org/packages/95/a6/aed1168f2b0477f84c500d957ceddaaa848cd24bd218c66dbde5dfc5ea95/django-custom-storage-0.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-23 22:18:55",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "DLRSP",
    "github_project": "django-custom-storage",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "django-custom-storage"
}
        
Elapsed time: 0.22315s