Hosted Videos Done Right

One of the great things about the internet is the amazing amount of free services. You can host hundreds of gigabytes on YouTube for free and there are many other sites that offer the same. But as we progress through the decade, I think companies are restricting access to their free services more and more. Stronger Content ID rules from sites like YouTube and Vimeo can be frusturating to work around.

Sometimes you don’t want the YouTube, Vimeo, or SoundCloud players on your website. There are many good reasons to host your own content and in this tutorial. I’ll be showing you how to host your video content on the Google Cloud. My method is a work in progress but its robust, secure, relatively cheap for small business, and infinitely scalable.

Contents

  • Summary
  • Tutorial
  • Video Tutorial
  • Podcast

What This is Not

This method is not a website CDN. yet. While I could use this method to store and serve my website .css .html .js and even images, I trust that part of my site to CloudFlare. CF however does not do well with media files. Videos, podcast episodes, large zip files: these are all integral parts of my blog and I plan to host more content like that in the future.

Think of this as a CDN for only media files.

This is also not a tutorial for setting up your own pirate movie theater. This content is hosted on Google Servers and files will be hashed. You will want to be careful what you put on your CDN.

Pricing

This tutorial is not for the average small blog like my own. You can expect to spend $60-200 a month depending on how much content you host and how visited your site is. Google Cloud will charge you for four services for a CDN setup like this, those being…

  • Load Balancing (~$30/month)
  • Static IP address (~$12/month)
  • 100gb Multi Regional Storage ($2.60/month)
  • 500gb Egress Traffic  ($32/month)

You can see that storing our actual content is very very cheap. You could store your videos on Google and directly link the resources to your website. I think most people will opt for this solution because its very cheap, you only pay for the storage and the traffic. But if you are a large video platform or really need to push videos to edge locations, you will need to fork over the money to run these infrastructure services.

The Delivery Breakdown

  1. User hits CF caches, content is delivered based on browser and edge caching settings via CF page rules.
  2. When expired headers are hit, CF will perform a revalidation.
  3. While the origin server and CF work together, my cdn media.whitkennedy.com will always be loading content on the page load. This means when a post is loaded, content is loaded from my google cdn and not the server nor CloudFlare.

The Cheap Way and the Right Way

Previous to my custom Google CDN, I’ve been using BackBlaze B2 for my podcast episodes and archive.org for hosting my favorite documentaries. I’ve also been using Amazon S3 to store my WordPress site backups. While B2 and archive.org are cheap, they are also slow and difficult to use. B2 needs more features (custom endpoints would be a start) and archive.org has some dog slow latency for anyone outside the bay area. You could lazy load your videos from archive.org but if your site blows up, I could see archive.org really slowing down your site.

You Have the Need For Speed

The Google Cloud is FAST. Google is investing millions in their platform and new features are rolled out regularly. I like the way GCS (Google Cloud Services) is organized and while it takes some getting used to for an AWS user, its nothing a few cups of coffee and a free evening can’t fix.

All that said, video content is heavy. By heavy I mean bandwidth intensive and geolocation-sensitive. You want to get video files as far away from your origin server as possible and closet to your users. We would never want to server a big HD video file from our origin server to a client thousands of miles away. That’s not how it’s done!

YouTube is Fine

This solution really depends on what your needs are. For most, YouTube, Vidme, Vimeo, and the rest will be fine. If you are trying to grow a following you will want to link to whatever platform you are on often. But there are situations where you don’t want to use their players. Vimeo blocks most music these days, YouTube blocks and claims stuff all day long, and Google Drive, Amazon Cloud Drive, SoundCloud, and tons of other services are getting expensive and quite narrowed in their functionality.

Blah Blah Blah Lets Make it Already…

Fine!


What You’ll Need

  • Google Cloud Console Account
  • Website to post content to
  • Some media files
  • Monthly budget of at least $50 for a small site.
  • Access to your domain DNS
  • Coffee, lots of coffee

Let’s Get Started

First, let’s create the project that we will be using

Next we need to create buckets to store our content in. I always use Multi Regional Buckets because their performance is awesome and they are the same cost as S3 Regional buckets (more or less).

This is what my bucket will look like for this demo

Accessing Your Files

There are 4 ways we can access our media files.

  1. https://storage.googleapis.com/media.example.com/
  2. https://storage.cloud.google.com/media.example.com/
  3. http://media.example.com
  4. https://media.example.com

For someone who just wants to host their files in google buckets and doesn’t care to setup a custom domain, you can use example 1 (storage.googleapis.com/example). This URL will be https and is the better option.

For those that want to server over http, you can setup a CNAME record for media.example.com by following this documentation.

But let’s continue by setting up https://media.example.com. We want both SSL while serving content, as well as a custom domain.

Setting Bucket Defaults

Before we dive into load balancing and SSL certificates, we need to setup our default permissions so that our files are accessible to the public. All you need to do is add this parameter to your bucket.

Edit object default permissions

Now our data can be directly accessed.

Upload some content now and make sure that public links are automatically turned on when a file is uploaded. If the public link is not checked, then the files will not be accessible.

Our files are now ready to be served. But we still need to get our custom domain name working. Let’s head over to the GCS networking panel and setup a Load Balancer.

Tutorials I followed

  1. Setting Up HTTP(S) Load Balancing
  2. Creating Content-Based Load Balancing
  3. Target Proxies

After some light reading. I headed into the Load Balancer tab and created my balancer.

We need to click Create a new certificate and then lets head to SSL For Free.

sslforfree is free but certs only last 90 days
download all your certs
upload them to this page

Now we can add our 90 Day Cert to our load balancer.

The last thing we need to do before testing out our domain is create a target proxy

Now we need to setup a Domain DNS record that follows the path of our Google Static IP

And now lets check our URL!

Balancing Complete

Now we can upload and server https content backed by very fast Google buckets. Our Load Balancer in Google CDN enabled and Google will now take care of where the media is coming from.

Lastly, I set up a page rule on CloudFlare to redirect all http traffic to https, this ensures that all content is server via https and that no mixed media errors will be displayed!

Caution

You will pay for the bandwidth served which can get very very expensive if you serve a lot of video content. Also note, if you have long live streams or videos hosted on your site, users that begin watching them will download the entire video while watching it. This is different from YouTube and Vimeo that buffers a few minutes ahead.

Be sure to renew your certificates every 90 days or you will have outages!


As always, please comment and corrections or suggestions down below. Thanks!