4

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,

Naveen
  • 677
  • 1
  • 11
  • 27

2 Answers2

4

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/'
Yarh
  • 895
  • 7
  • 15
  • Can you please elaborate this a little for Google Compute Engine ? I tried running 'python -c 'import os; print(os.getenv('SERVER_SOFTWARE', ''))' and got "name 'SERVER_SOFTWARE' is not defined" as the error on Google Compute Engine VM Instance. – Naveen Oct 03 '16 at 04:51
  • If you run the local development server [link](https://cloud.google.com/appengine/docs/python/tools/using-local-server) then it should add 'SERVER_SOFTWARE' environment variable to os package. when it runs locally than the software version will be something with "dev", but if it runs on GAE, it will be 'Google App Engine' – Yarh Oct 03 '16 at 17:55
1

While this isn't a Django-based answer, since I know little about that, you may find the Alpha release of Google Cloud Load Balancer support for Google Cloud Storage another route to provide URL maps to static content in GCS while serving the rest of your data on GCE.

Nathan Herring
  • 977
  • 8
  • 16