- Leveraging CDN with Google Cloud Storage and Cloud Run - Part 2
- This is the second article on how to build a CDN using Google Cloud Storage and Cloud Run. It explains how to optimize the delivery of images and text files and distribute resources across 8 global regions to reduce latency.
This post has been translated by AI.
Post summarized by durumis AI
- This article explains how to optimize images uploaded to Google Cloud Storage using Cloud Run and convert them into various formats.
- Leveraging file upload and deletion events in Cloud Storage, we will convert image files to AVIF and WebP formats, compress text files, and synchronize replication and deletion to other regions.
- This allows for efficient management of image optimization and file synchronization in a CDN environment, and we will preview the subsequent content utilizing Google Cloud Load Balancer.
In the previous post, we discussed how to use Cloud Run to transmit image files synchronized to GCS.
So, in the final part of this CDN series, let's discuss GCS synchronization and image optimization.
Cloud Run primarily operates through HTTP hooks, but this isn't the only event trigger available.
Cloud Run
When setting up Cloud Run, you'll notice a section called 'Trigger'. If you check the list there, you'll find Pub/Sub, Cloud Storage, Filestore, and others. Excluding the default HTTP, the rest are the most commonly used products. Additionally, most other conceivable events are included under 'Others'. Let's choose the Cloud Storage event, as we'll be using it immediately. When you do, you'll see a list of event types like file changes, file deletion, file creation, and metadata changes.
Cloud Run Eventarc
Here, by utilizing the 'deleted' and 'finalized' events, we can create a Cloud Run instance that handles events triggered when files are uploaded to or deleted from GCS.
The tasks that Cloud Run needs to handle are mainly as follows:
- When a file is uploaded, it checks the file type and then performs the following processes.
- If it's a text file (css, js), it compresses the file using br and gzip to create a new file.
- If it's an image file, it uses sharp npm to create converted formats like avif and webp.
- The newly created file is replicated to various other 'regions' provided.
- Upon receiving a file deletion request, it also deletes the corresponding file in other regions.
Basically, if you handle these three tasks (conversion, replication, and deletion) properly, file synchronization across multiple regions won't be very difficult. Even in the code, you can easily handle it using just two npm packages: one for connecting to GCS and the other for using sharp.
This concludes our basic CDN series of posts.
Starting with the next post, we'll delve into GCLB (Google Cloud Load Balancer).
While a CDN discussion naturally involves load balancers (LB), some aspects were omitted in this series. We'll cover them in detail in the following posts.
Thank you.