How to set up Google Cloud Storage on a Django web application
It goes without mentioning that any non-trivial web application will require file storage capabilities of some sort. Django is a highly efficient web framework. Google Cloud Storage offers efficient object storage capacities for a wide range of needs. Placing these two technologies side-by-side offers developers a perfect union of scalable storage.
Setting up the environment
Package | Purpose |
django | Web application framework |
django-environ | Environment variables management |
django-storages, google-cloud-storage | Django File API for Google Cloud Storage |
Certain packages are essential in order to properly link a Django web application and Google Cloud Storage. The following command includes all essential installs.
pip install django django-environ django-storages google-cloud-storage
Configuring Google Cloud Storage
The instructions here give the steps to follow in a general sense.
I will walk you through what you need to do to get your Django web application connected as smoothly as possible.
Save the json file created. Keep this in a safe place, as the information it contains is important in making the connection.
Click on the menu icon via the Google Cloud Web Console. Follow the path
Storage > Browser
, and click the CREATE BUCKET button.Edit your
settings.py
file to include the following imports.
import json
from google.oauth2 import service_account
Next, you need to set the default file storage for your Django application, and the Google Cloud Storage bucket name.
- Note that it is best to save your bucket name as an environment variable, due to its sensitive nature.
DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
GS_BUCKET_NAME = env('STORAGE_BUCKET_NAME')
- Next, you need to set up your Google Cloud Storage credentials, which heavily relies on the information in the service account created earlier.
Safely use Google Cloud Storage credentials
You would find that the django-storages
documentation advises the following format.
# do not use this!
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
"path/to/credentials.json"
)
# do not use this!
I would advise against this - because it requires committing your credentials.json
file to source code, and can cause more problems than it solves.
This particular challenge bothered me for a while. To better understand it, I went into the service_account
file in the Google Auth Library Python source code on Github.
I found that while the library suggested the from_service_account_file
function in the Credentials class (lines 216 to 230), I could easily change this to the from_service_account_info
function located directly above it (lines 195 to 213).
The main difference between how the two functions operate is that the ...account_file
requires the path to a json file, while ...account_info
simply requires a json object.
Using the peculiarity of the from_service_account_info
function to our benefit, we can simply save the credentials json object as an environment variable.
Upon storing the environment variable in string format we can then transform it back into a json object, ready for use in our modified GS_CREDENTIALS
- adding scope to highlight desired functionality.
gcp_cred = env('GOOGLE_CLOUD_CREDENTIALS')
info = json.loads(gcp_cred)
scope = 'https://www.googleapis.com/auth/devstorage.read_write'
GS_CREDENTIALS = service_account.Credentials.from_service_account_info(
info, scopes=(scope,)
)
With this configuration in your settings.py
file, you are ready and all set up to use Google Cloud Storage in your Django web application.
Cover base from Unsplash
Comments (2)