From d299172784612e0741dafb5df160c103872977be Mon Sep 17 00:00:00 2001 From: jaikishanwadhawa Date: Wed, 26 Feb 2025 10:53:49 +0530 Subject: [PATCH] add support for gcp bucket for media (#529) Co-authored-by: Jai Co-authored-by: Horilla <131998600+horilla-opensource@users.noreply.github.com> --- .env.dist | 9 +++++++++ horilla/horilla_backends_gcp.py | 23 +++++++++++++++++++++++ horilla/horilla_settings.py | 8 +++++++- requirements.txt | 1 + 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 horilla/horilla_backends_gcp.py diff --git a/.env.dist b/.env.dist index 8684675c0..b299718ca 100644 --- a/.env.dist +++ b/.env.dist @@ -25,6 +25,15 @@ DB_PASSWORD=password DB_HOST=localhost DB_PORT=5432 +# GCP Storage for Media Configuration +# GOOGLE_APPLICATION_CREDENTIALS="pathToYourServiceAccountJsonFile" +# GS_BUCKET_NAME="yourGCPBucketName" +# DEFAULT_FILE_STORAGE="horilla.horilla_backends_gcp.PrivateMediaStorage" +# MEDIA_URL="https://storage.cloud.google.com/yourGCPBucketName/media" +# MEDIA_ROOT="https://storage.cloud.google.com/yourGCPBucketName/media" +# NAMESPACE="private" + + # Supportted Formats for DATABASE_URL : diff --git a/horilla/horilla_backends_gcp.py b/horilla/horilla_backends_gcp.py new file mode 100644 index 000000000..ec031b145 --- /dev/null +++ b/horilla/horilla_backends_gcp.py @@ -0,0 +1,23 @@ +""" +horilla/horilla_backends_gcp.py +""" + +from django.db import models +from storages.backends.gcloud import GoogleCloudStorage + +from horilla import settings + + +class PrivateMediaStorage(GoogleCloudStorage): + """ + PrivateMediaStorage + """ + + location = settings.env("NAMESPACE", default="private") + default_acl = "private" + file_overwrite = False + + +# To set the private storage globally +models.FileField.storage = PrivateMediaStorage() +models.ImageField.storage = PrivateMediaStorage() diff --git a/horilla/horilla_settings.py b/horilla/horilla_settings.py index 3fa55eb47..f29b56217 100644 --- a/horilla/horilla_settings.py +++ b/horilla/horilla_settings.py @@ -123,7 +123,13 @@ if settings.env("AWS_ACCESS_KEY_ID", default=None): settings.DEFAULT_FILE_STORAGE = DEFAULT_FILE_STORAGE settings.AWS_S3_ADDRESSING_STYLE = AWS_S3_ADDRESSING_STYLE +if settings.env("GOOGLE_APPLICATION_CREDENTIALS", default=None): + GS_BUCKET_NAME = settings.env("GS_BUCKET_NAME") + DEFAULT_FILE_STORAGE = settings.env("DEFAULT_FILE_STORAGE") -if settings.env("AWS_ACCESS_KEY_ID", default=None) and "storages" in INSTALLED_APPS: + settings.GS_BUCKET_NAME = GS_BUCKET_NAME + settings.DEFAULT_FILE_STORAGE = DEFAULT_FILE_STORAGE + +if (settings.env("GOOGLE_APPLICATION_CREDENTIALS", default=None) or settings.env("AWS_ACCESS_KEY_ID", default=None)) and "storages" in INSTALLED_APPS: settings.MEDIA_URL = f"{settings.env('MEDIA_URL')}/{settings.env('NAMESPACE')}/" settings.MEDIA_ROOT = f"{settings.env('MEDIA_ROOT')}/{settings.env('NAMESPACE')}/" diff --git a/requirements.txt b/requirements.txt index fb676b40d..11020a8bd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -75,3 +75,4 @@ XlsxWriter gunicorn psycopg2-binary whitenoise +django-storages