[Django]-How to use django-google-cloud-storage for GCS in Django on GAE

3👍

Like the way you would use any other File Storage System in Django, because custom storage backends in Django are built by defining a custom storage class which must be a subclass of django.core.files.storage.Storage.

django-cloud-storage , is a custom storage backend which allows you to use GCS in Django, just like you would if you were using any other storage backend.

DEFAULT_FILE_STORAGE = 'google.storage.googleCloud.GoogleCloudStorage'

The above line in your settings.py makes GCS your default File storage system.
That means if you define a new FileField in your models, it will use Google Cloud Storage.

You can use it in your views like this:

from django.core.files.storage import default_storage
file = default_storage.open(file_name, 'w')
file.write(output.read())
file.close()

The file will be saved to your default bucket.

Leave a comment