Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Settings for Rendering static images from Google Cloud storage

I have my Django App hosted on Google Compute Engine. I wish to render static elements of the App from Google Cloud Storage. I have all the static elements inside Google Cloud storage bucket www.example.com/static

My Settings.py:

# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '../example_static')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, '../example_media')
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'), MEDIA_ROOT,)

000-default.conf File:

<VirtualHost *:80>
    .....
    DocumentRoot /var/www/html

    Alias /static /opt/projects/example-google/example_static
    ....
</VirtualHost>

With Current settings, it is picking up the static files from path: /opt/projects/example-google/example_static.

Can someone please explain the settings change required for rendering all the static images from Google Cloud storage bucket www.example.com/static ?

Thanks,

like image 225
Naveen Avatar asked Sep 18 '25 12:09

Naveen


1 Answers

You can find some documentation here

One more thing i found useful is automatically switching between dev and prod environments by doing to following changes in your app settings.py:

if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
    STATIC_URL = 'https://storage.googleapis.com/<your-bucket>/static/'
else:
    STATIC_URL = '/static/'
like image 177
Yarh Avatar answered Sep 21 '25 02:09

Yarh