1
votes

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 ?

2

2 Answers

9
votes

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

2
votes

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