add support for gcp bucket for media (#529)

Co-authored-by: Jai <jai@voicegain.ai>
Co-authored-by: Horilla <131998600+horilla-opensource@users.noreply.github.com>
This commit is contained in:
jaikishanwadhawa
2025-02-26 10:53:49 +05:30
committed by GitHub
parent 62b60d8a20
commit d299172784
4 changed files with 40 additions and 1 deletions

View File

@@ -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 :

View File

@@ -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()

View File

@@ -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')}/"

View File

@@ -75,3 +75,4 @@ XlsxWriter
gunicorn
psycopg2-binary
whitenoise
django-storages