Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file storage option #12590

Draft
wants to merge 10 commits into
base: release-v0.17.x
Choose a base branch
from
13 changes: 13 additions & 0 deletions kolibri/deployment/default/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"


# File Storage Backend
# https://docs.djangoproject.com/en/3.2/ref/files/storage/

if not os.environ.get("DEFAULT_FILE_STORAGE"):
if conf.OPTIONS["FileStorage"]["STORAGE_BACKEND"] == "file_system":
DEFAULT_FILE_STORAGE = "django.core.files.storage.FileSystemStorage"
elif conf.OPTIONS["FileStorage"]["STORAGE_BACKEND"] == "gcs":
# https://django-storages.readthedocs.io/en/latest/backends/gcloud.html#google-cloud-storage
GS_DEFAULT_ACL = "publicRead"
DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
# GS_PROJECT_ID, GS_CREDENTIALS, etc should be inferred from the environment


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

Expand Down
30 changes: 30 additions & 0 deletions kolibri/utils/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,24 @@ def multiprocess_bool(value):
return False


def storage_option(value, *opts):
"""
Validate the storage options.
Check that the given option is valid, then check that needed external
libraries are available where relevant.
"""
value = is_option(value, *opts)
if value == "gcs":
try:
from storages.backends.gcloud import GoogleCloudStorage # noqa
except ModuleNotFoundError:
logger.error(
"Google Cloud Storage backend is not available.",
"Are storage requirements installed?",
)
raise VdtValueError(value)


def cache_option(value):
"""
Validate the cache options.
Expand Down Expand Up @@ -359,6 +377,17 @@ def lazy_import_callback_list(value):


base_option_spec = {
"FileStorage": {
"STORAGE_BACKEND": {
"type": "storage_option",
"options": ("file_system", "gcs"),
"default": "file_system",
"description": """
The storage backend class that Django will use when managing files. The class given here must implement
the Django files.storage.Storage class.
""",
},
},
"Cache": {
"CACHE_BACKEND": {
"type": "cache_option",
Expand Down Expand Up @@ -737,6 +766,7 @@ def _get_validator():
"url_prefix": url_prefix,
"bytes": validate_bytes,
"multiprocess_bool": multiprocess_bool,
"storage_option": storage_option,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how will this work with database based cache?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm - I'm not sure. I assumed this was only related to validation on initialization

"cache_option": cache_option,
"lazy_import_callback_list": lazy_import_callback_list,
}
Expand Down
2 changes: 2 additions & 0 deletions requirements/storages.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Additional reqs for running kolibri with GCS file storage backend
django-storages[google]==1.14.2
Loading