django-storages *************** - :ref:`digital-ocean-spaces` Configuration ============= Add ``django-storages`` with ``s3`` to ``pyproject.toml``:: uv add django-storages[s3] --group dev Add ``storages`` to ``THIRD_PARTY_APPS`` in ``src/settings/base.py``:: THIRD_PARTY_APPS = ( # ... "storages", Add the following to production settings (``src/settings/production.py``):: AWS_S3_ACCESS_KEY_ID = get_env_variable("AWS_S3_ACCESS_KEY_ID") AWS_S3_SECRET_ACCESS_KEY = get_env_variable("AWS_S3_SECRET_ACCESS_KEY") AWS_STORAGE_BUCKET_NAME = get_env_variable("AWS_STORAGE_BUCKET_NAME") AWS_S3_ENDPOINT_URL = get_env_variable("AWS_S3_ENDPOINT_URL") AWS_S3_REGION_NAME = get_env_variable("AWS_DEFAULT_REGION") STORAGES = { "default": {"BACKEND": "base.storage.MediaStorage"}, "private": {"BACKEND": "base.storage.PrivateStorage"}, "staticfiles": {"BACKEND": "base.storage.StaticStorage"}, } Development =========== Create ------ To create a file on Digital Ocean Spaces / S3:: from base.storage import PrivateStorage from django.http import HttpResponseRedirect storage = PrivateStorage() file_name = storage.save(f"{folder}/{pdf_file_name}", content_file) url = storage.url(file_name) return HttpResponseRedirect(url) Download (for the user) ----------------------- :: from base.storage import is_aws from django.http import HttpResponseRedirect if is_aws(): return HttpResponseRedirect(schedule.output_file.url) .. tip:: For sample code, see the ``download_report`` function in the report app (``src/report/views.py``). Download (for processing) ------------------------- To download a file for processing from the Digital Ocean Spaces or Amazon S3, check the `download_csv_from_s3`_ function in the report app (``src/report/views.py``). ``is_aws`` ---------- The ``is_aws`` method checks the ``AWS_S3_ACCESS_KEY_ID`` setting:: from base.storage import is_aws if is_aws(): # do something... pass .. _`download_csv_from_s3`: https://gitlab.com/kb/report/-/blob/7043-pyproject-toml/src/report/views.py?ref_type=heads#L35