[Django]-How to configure django to generate signed urls for media files with Google Cloud CDN?

4๐Ÿ‘

โœ…

I ended up writing a storage backend. The settings.CUSTOM_DOMAIN is the address of the CDN. My backend code is as follows โ€“

class CustomGoogleCloudStorage(GoogleCloudStorage):
    custom_domain = settings.CUSTOM_DOMAIN

    def make_url(self, name):
        host = "https://" + self.custom_domain
        return urllib.parse.urljoin(host, name)

    def url(self, name):
        name = self._normalize_name(name)
        url = self.make_url(name)
        key_name, base64_key = get_storage_key()
        expiration_time = datetime.datetime.utcnow() + datetime.timedelta(hours=1) #put your desired time here
        return sign_url(url, key_name, base64_key, expiration_time)

The sign_url function is taken from GCPโ€™s github.

๐Ÿ‘คalamshafi2263

2๐Ÿ‘

Django is a high-level Python Web framework. You might use the Cryptographic signing feature used by Django with the signed URLs Python (Programmatically creating signed URLs) described in Cloud CDN documentation. Take a look on both links and let us know if that helps to answer your inquiry.

๐Ÿ‘คuser10880591

Leave a comment