1

I have several video content that I share through my Google Cloud Storage through my Django Google App Engine Application with signed url mechanism associated with an expiration time.

def get_signed_url(self, entity):
    blob = bucket.get_blob(entity.url)
    logging.info(entity.url)
    expiration_time = timezone.now() + timedelta(minutes=120)
    signed_url = blob.generate_signed_url(expiration_time)
    logging.info(signed_url)

    return signed_url

Although it has been explained in [here][1] possible usage relationship of GCS and Google Cloud CDN, would this be applicable to stream video content (MP4 or MPEG-DASH with MP4) through Google Cloud Storage as it is mentioned to have an implicit CDN itself.

If using Google CDN is a wiser way to broadcast online video content, what would be the best strategy to achieve this, how can I use the Google Cloud CDN on top of my current implementation with Google Cloud Storage ?

london_utku
  • 1,070
  • 2
  • 16
  • 36

2 Answers2

15

Although Google Cloud Storage leverage "parts" of the CDN infrastructure, it is only a convenience layer, with no control over cache keys, invalidation and requires either a public bucket (at odds with signed URLs), or per-object Cache-Control metadata to be set.

Egressing large volumes of data (e.g. HLS/DASH video) from a bucket is also a fair bit more expensive - for North America, it ranges from $0.12 - $0.08 based on volume. Cloud CDN ranges from $0.08 - $0.02 (after a petabyte) for North America egress.

You should also take a look at the Signed URLs and Signed Cookies support in Cloud CDN, which allows you protect your video segments from unauthorized access on a per-user basis - similar to GCS signed URLs.

TL;DR: GCS & its caching mode are a nice convenience and good for small traffic volumes, but if you plan to even serve a few hundred GB (let alone more), setting up a HTTPS Load Balancer + Cloud CDN in front of a bucket will give you a lot more flexibility and reduced costs.

(I am the PM for Cloud CDN, if it helps!)

elithrar
  • 23,364
  • 10
  • 85
  • 104
  • you mean to say " setting up a HTTPS Load Balancer + Cloud CDN in front of a bucket" you mean to say in front of Object store bucket?.. – MrSham Jul 02 '21 at 06:17
3

To answer your question of how can I use the Google Cloud CDN on top of my current implementation with Google Cloud Storage on your django. You can base your implementation below with django-storages

requirements.txt (the versions are the latest up to this day: 05-31-2020)

...
django-storages==1.19.1
google-cloud-storage==1.28.1

example/example/settings.py

...
...
SB_SA_FILE = os.environ.get('STORAGE_BUCKETS_FILE',
                            'storageBucketsBackendServiceKey.json')
STATICFILES_STORAGE = 'example.lib.storages.GoogleStaticFilesStorage'  # static
DEFAULT_FILE_STORAGE = 'example.lib.storages.GoogleMediaFilesStorage'  # media
GS_AUTO_CREATE_BUCKET = True
GS_DEFAULT_ACL = 'publicRead'
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
    f'/usr/src/app/{SB_SA_FILE}'
)
GS_BUCKET_NAME = os.environ.get('GS_BUCKET_NAME')
CDN_HOSTNAME = os.environ.get('CDN_HOSTNAME', '')

example/example/lib/storages.py

from django.conf import settings
from storages.backends.gcloud import GoogleCloudStorage



class GoogleMediaFilesStorage(GoogleCloudStorage):

    def _save(self, name, content):
        name = f'{settings.MEDIA_URL[1:]}{name}'
        return super()._save(name, content)

    def url(self, name):
        """
        @brief      for implementation of CDN using image field url
        @return     Dynamic return of CDN or local URL
        """
        if settings.CDN_HOSTNAME:
            url = f'{settings.CDN_HOSTNAME}/{name}'
            return url
        return super().url(name)


class GoogleStaticFilesStorage(GoogleCloudStorage):
    def url(self, name):
        name = f'static/{name}'
        if settings.CDN_HOSTNAME:
            url = f'{settings.CDN_HOSTNAME}/{name}'
            return url
        return super().url(name)

Lastly, you need to run your django application with a CDN_HOSTNAME environment variable. The value of CDN_HOSTNAME environment variable must be the domain mapped in your Google Cloud Global Load Balancer which your desired Cloud Storage is set as a backend bucket

Dean Christian Armada
  • 6,724
  • 9
  • 67
  • 116
  • Thanks Dean, that is greatly appreciated. What about using signed urls and access restrictions ? How can I use them Google CDN ? – london_utku May 30 '20 at 10:50
  • 1
    Hi @SuperEye, I have never tried using signedURLs.. But according to the documentation here: https://django-storages.readthedocs.io/en/latest/backends/gcloud.html You just need to remove `GS_DEFAULT_ACL = 'publicRead'` – Dean Christian Armada May 31 '20 at 02:30
  • But, this does not provide a mechanism to restrict access to certain files. To achieve this, signed_url mechanism should be employed. – london_utku Jun 01 '20 at 00:36
  • 1
    Oh.. Particular files only.. So yes, I guess you are correct.. You may need to put some logic on your django application for your case – Dean Christian Armada Jun 01 '20 at 02:39
  • Hi @Dean, and as far as I can see, in your suggestion whole data will pass through the application here. I am looking for a way which the user will directly communicate to the Storage/CDN not via the application, where it will have Google storage as origin, Google CDN as CDN while having restiricted access via signed urls. – london_utku Jun 01 '20 at 09:35
  • 1
    It should communicate directly through the Storage/CDN. Let us say you have an API that returns images. The Image URL that will be passed on the client let us say a browser will be your URL of image hosted in either your Storge/CDN – Dean Christian Armada Jun 01 '20 at 10:06