1

I am still new with serving my Django static/media files to Google cloud storage. It's working now but I am not sure if this is right or is this enough already, do I still need to use CDN like Cloudfront or other similar services? I am really confused and any recommendation would be much appreciated.

Below is my configuration.

import os
from google.oauth2 import service_account

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
os.path.join(BASE_DIR, 'extras/google-storage.json')
)

STATICFILES_STORAGE = 'extras.storage_backends.GoogleCloudStaticFileStorage'
DEFAULT_FILE_STORAGE = 'extras.storage_backends.GoogleCloudMediaFileStorage'

GS_PROJECT_ID = 'project_id'
GS_STATIC_BUCKET_NAME = 'static_bucket'
GS_MEDIA_BUCKET_NAME = 'media_bucket'


STATIC_URL = 'https://storage.googleapis.com/{}/'.format(GS_STATIC_BUCKET_NAME)
MEDIA_URL = 'https://storage.googleapis.com/{}/'.format(GS_MEDIA_BUCKET_NAME)
GS_DEFAULT_ACL = 'publicRead'

I am using the following:

Django 2.2

Python 3.7

Thanks you so much!

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
aldesabido
  • 1,268
  • 2
  • 17
  • 38

3 Answers3

1

You won't need any further services like CDN or CloudFront for your Django media to be used with Cloud Storage. As clarified in the official Django Storage documentation - accessible here - there are a few parameters that you need to have configured to integrate the products and it doesn't include network services.

Another example of how integrate the two and where this type of service is not recommended is in this article here. So, to summarize, don't worry, you won't need this type of network service to continue running your Django files to Cloud Storage.

gso_gabriel
  • 4,199
  • 1
  • 10
  • 22
  • Thank you for your answer @gso. I have another question, if ever I'll transfer from Google cloud to AWS S3, should I need CDN or CloudFront for my media? – aldesabido Sep 15 '20 at 04:00
  • Hi @aldesabido glad to hear it helped you! As per this answer [here](https://stackoverflow.com/a/39333278/12767257) from an engineer from GCS, you won't need CDN or CloudFront for that. You can use `gsutil` commands to perform the copy/transfer of the data for you. – gso_gabriel Sep 15 '20 at 07:34
  • Sorry for my misleading question. What I mean is when I use S3 instead of GCS in my Django app, should I need to use Cloudfront? My initial conclusion was: If GCS no need for CloudFront but if S3 need to have a Cloudfront. – aldesabido Sep 15 '20 at 12:08
  • No worries. Unfortunately, AWS is not my specialty. I would recommend you to raise a new question, so you can receive the answer from a person that have more knowledge on it. :) – gso_gabriel Sep 15 '20 at 12:35
0

i had the same exact question, and i had it marked until i resolved everything. This is the full explanation, hope it helps and people from google find it usefull:

This is the part of my Settings file where i use the google buckets

## GETTING THE CREDENTIALS OF THE ACCOUNT ##

from google.oauth2 import service_account
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(os.path.join(BASE_DIR,"credentials.json"))  

## STATIC FILES ##  

STATICFILES_FINDERS = ('django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', )   

STATICFILES_DIRS =[os.environ['STATIC_URL'],] 
STATIC_ROOT = '/static/' 
STATIC_URL = os.environ['STATIC_URL'] 
 

#GS_STATIC_BUCKET_NAME = os.environ["GS_STATIC_BUCKET_NAME"] 
#STATICFILES_STORAGE = "website.custom.GoogleCloudStaticStorage"  

## MEDIA FILES ##  
GS_BUCKET_NAME = os.environ['GS_MEDIA_BUCKET_NAME'] 
GS_PROJECT_ID = os.environ['GS_PROJECT_ID'] 
GS_DEFAULT_ACL = 'publicRead'      
GS_FILE_CHARSET = True #This changes the names of the files
GS_FILE_OVERWRITE = False #no overwritting, is important, you don't know if your users have file 1.png and mess up your page
DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'   
MEDIA_ROOT = 'media' 
MEDIA_URL = os.environ['MEDIA_URL'] 

i would use a custom class for file storage because idk what your skills are, but for me, no overwritting is a must, and why code it if the package already does that. Maybe a custom upload for static files because they do get re written a lot, so maybe that's something you can leave.

Im currently working on signals to resize the uploaded images, which is a great feature, because of the 5gigs free space per project.

Hope it helps you. Have a great day.

  • Thank you for your answer @jacob. I have another question. Are you using uWSGI and Nginx in your production? – aldesabido Sep 15 '20 at 03:58
  • @aldesabido im using Nginx, the same as the google app engine tutorial. – jacobitosuperstar Sep 15 '20 at 17:00
  • @aldesabido the tutorial lines where: # the PROJECT-DIRECTORY is the one with settings.py and wsgi.py entrypoint: gunicorn -b :$PORT website.wsgi – jacobitosuperstar Feb 23 '21 at 07:23
  • @aldesabido sorry for not answering before, i don't know how to put notifications on this page. The answeris an strong NO. With the google app engine we have several free 5 gig buckets per proyect that i use to serve my static files as a CDN. Because of that i prefer to deal with static URLs than to use Ngnix... the only part is that it fucks up development if you don't have several development branches for local and for cloud, but I'm never going back to Nginx – jacobitosuperstar Aug 02 '21 at 19:09
-1

If the page loading time of your website is less than 2 seconds, basically you don't need CDN. Of course, if you want shorter page loading time, you can use CloudFront on AWS and Cloud CDN on GCP. In addition, if your website uses database, you can also use ElastiCache on AWS and Memorystore on GCP for shorter page loading time.

With the Chrome Extension "Page load time", I checked the page loading time for several websites such as YouTube, Amazon Online Shop, Facebook, Twitter, Stack Overflow, Github, Linkedin, CNN and Quora as shown below.

<0.0 ~ 0.5 seconds>

  • Twitter

<Around 1.0 seconds>

  • Github
  • Stack Overflow
  • Linkedin
  • CNN
  • Quora

<1.5 ~ 2.0 seconds>

  • YouTube

<Around 2.0 seconds>

  • Facebook

<2.0 ~ 2.5 seconds>

  • Amazon Online Shop
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129