Search code examples
djangogoogle-cloud-platformdjango-rest-frameworkgoogle-cloud-storage

How to increase expiry date/time of Google Cloud Storage Objects in Django


I have a Django REST API that uses the storages.backends.gcloud.GoogleCloudStorage as its DEFAULT_FILE_STORAGE. I noticed that media files (e.g. profile pictures of users) retrieved from any API has an Expires value in the URL.

Here's a sample URL to a file: https://storage.googleapis.com/<BUCKET_NAME>/users/a96db2f0-99a0-4691-aadb-708dc5268f77.png?Expires=1597840065&GoogleAccessId=<ACCESS_ID>&Signature=<SIGNATURE>

If I convert that to an actual date, it's actually 24 hours from the time it was requested. How do I increase that to a very big time (e.g. 1 year)?


Solution

  • It turns out, it's all stated in their documentation: https://django-storages.readthedocs.io/en/latest/backends/gcloud.html

    GS_EXPIRATION (optional: default is timedelta(seconds=86400))

    The time that a generated URL is valid before expiration. The default is 1 day. Public files will return a url that does not expire. Files will be signed by the credentials provided to django-storages (See GS_CREDENTIALS).

    Note: Default Google Compute Engine (GCE) Service accounts are unable to sign urls.

    The GS_EXPIRATION value is handled by the underlying Google library. It supports timedelta, datetime, or integer seconds since epoch time.

    So I just do GS_EXPIRATION = timedelta(seconds=864000) in the settings.py so that the expiry would be 10 days.