Search code examples
pythondjangogoogle-app-enginegoogle-cloud-storagedjango-storage

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


How do I use a Django Custom Storage backend with Google Cloud Storage?

Trying to use this: ckopanos's django-google-cloud-storage

As per the documentation I have added the following to my 'settings.py':

GOOGLE_CLOUD_STORAGE_BUCKET = [my bucket name] # the name of the bucket you have created from the google cloud storage console
GOOGLE_CLOUD_STORAGE_URL = [my bucket URL] #whatever the url for accessing your cloud storgage bucket
GOOGLE_CLOUD_STORAGE_DEFAULT_CACHE_CONTROL = [cache control] # default cache control headers for your files

DEFAULT_FILE_STORAGE = 'google.storage.googleCloud.GoogleCloudStorage'

And I added the 'google' folder/package to my project files so it should be accesible for 'settings.py'

How do I use this Custom Storage System in my views now?

I can manually use gcs by doing the following (this example stores an xlsx on gcs):

import cloudstorage as gcs
from google.appengine.api import app_identity
import xlsxwriter
import StringIO

bucket_name = os.environ.get('BUCKET_NAME',
                           app_identity.get_default_gcs_bucket_name())
bucket = '/' + bucket_name

filename = bucket + '/'+ userid + '/' + [some code] + '.xlsx' 
gcs_file = gcs.open(filename,
            'w',
            content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            options={'x-goog-meta-user': userid})

output = StringIO.StringIO()
workbook = xlsxwriter.Workbook(output, {'in_memory': True})
worksheet = workbook.add_worksheet()

worksheet.write(0, 0, 'Hello World')

# Close the workbook before streaming the data.
workbook.close()

# Rewind the buffer.
output.seek(0)            

gcs_file.write(output.read())
gcs_file.close()

How do I import the storage system for use in my views? Would you reccomend a different storage system for Django and GCS instead?


Solution

  • 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.