Jude Maranga
Jude Maranga

Reputation: 886

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)?

Upvotes: 1

Views: 1467

Answers (2)

RealScatman
RealScatman

Reputation: 376

If the permissions for your bucket on Google Cloud Storage is set to public access (fine-grained required), you can set GS_DEFAULT_ACL = 'publicRead' in your settings.py file and the urls will be returned as public URLs which do not expire.

A sample public URL to a file would be:

https://storage.googleapis.com/<BUCKET_NAME>/users/a96db2f0-99a0-4691-aadb-708dc5268f77.png

Hope this helps anyone that stumbles upon this issue.

Upvotes: 0

Jude Maranga
Jude Maranga

Reputation: 886

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.

Upvotes: 1

Related Questions