'Google Cloud Storage doesn't upload to the correct media and static directory in the bucket

I have created an app in Django and want to use Google Cloud Storage to keep my statics and media files. However, when running python manage.py collectstatic

It uploads all the statics to the google cloud bucket root instead of the directory "static" in the bucket.

This is my core/settings/local.py

from .base import *  # noqa

ALLOWED_HOSTS = ["*"]
DEBUG = True

INSTALLED_APPS += ["django_extensions", ]

DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
STATICFILES_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"

GS_BUCKET_NAME = "name-bucket"

and here is my core/settings/base.py

import os
from pathlib import Path

import dj_database_url
from decouple import config

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent
 
ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "whitenoise.runserver_nostatic",
    "django.contrib.staticfiles",
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

STATIC_FILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = "/static/"
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

STATICFILES_FINDER = [
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
]
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"

MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"

How can I get to upload my statics to a folder called "static" in my bucket instead in the root? enter image description here



Solution 1:[1]

You need to create "gcsUtils.py" in "core" folder where "settings.py" is:

enter image description here

Then, put this code below to "gcsUtils.py" to define "Static" and "Media" variables which each have a "GoogleCloudStorage" class object:

# "core/gcsUtils.py"

from storages.backends.gcloud import GoogleCloudStorage

Static = lambda: GoogleCloudStorage(location='static')
Media = lambda: GoogleCloudStorage(location='media')

Then, replace your code below in "settings.py":

# "core/settings.py"

DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
STATICFILES_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"

With this code below:

# "core/settings.py"

DEFAULT_FILE_STORAGE = 'core.gcsUtils.Media'
STATICFILES_STORAGE = 'core.gcsUtils.Static'

Then, run this command below again:

python manage.py collectstatic

Now, "static" folder is created in "my-django-bucket":

enter image description here

And static files are collected from "admin" and "application" folders to "static" folder in "my-django-bucket":

enter image description here

You can see the full instruction of Django and GCS bucket.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kai - Kazuya Ito