'Django Static - Google Cloud Storage - CDN
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!
Solution 1:[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.
Solution 2:[2]
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.
Solution 3:[3]
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>
<Around 1.0 seconds>
- Github
- Stack Overflow
- CNN
- Quora
<1.5 ~ 2.0 seconds>
- YouTube
<Around 2.0 seconds>
<2.0 ~ 2.5 seconds>
- Amazon Online Shop
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 | gso_gabriel |
Solution 2 | jacobitosuperstar |
Solution 3 |