Serving files from Cloud Storage
Cloud Storage can host static assets for dynamic web apps. The benefits of using Cloud Storage instead of serving directly from your app include:
- Cloud Storage essentially works as a content delivery network. This does not require any special configuration because by default any publicly readable object is cached in the global Cloud Storage network.
- Your app's load will be reduced by offloading serving static assets to Cloud Storage. Depending on how many static assets you have and the frequency of access, this can reduce the cost of running your app by a significant amount.
- Bandwidth charges for accessing content can often be less with Cloud Storage.
The Google Cloud Client Library provides an idiomatic .NET client to Cloud Storage, for storing and retrieving data with Cloud Storage in an App Engine app.
Example of serving from a Cloud Storage bucket
This simple example creates a Cloud Storage bucket and uploads static assets using Cloud SDK:
Create a bucket. It's common, but not required, to name your bucket after your project ID. The bucket name must be globally unique.
gsutil mb gs://<your-bucket-name>
Set the ACL to grant read access to items in the bucket.
gsutil defacl set public-read gs://<your-bucket-name>
Upload items to the bucket. The
rsynccommand is typically the fastest and easiest way to upload and update assets. You could also use
gsutil -m rsync -r ./static gs://<your-bucket-name>/static
You can now access your static assets via
For more details on how to use Cloud Storage to serve static assets, including how to serve from a custom domain name, refer to How to Host a Static Website.
Serving files from other GCP services
You also have the option of using Cloud CDN or other GCP storage services.
Serving files directly from your app
Serving files from your app is typically straightforward, however, there are a couple drawbacks that you should consider:
- Requests for static files can use resources that otherwise would be used for dynamic requests.
- Depending on your configuration, serving files from your app can result in response latency, which can also affect when new instances are created for handling the load.
Example of serving static files with your app
<html> <head> <meta charset="utf-8" /> <title>Hello Static World</title> </head> <body> <p>This is a static html document.</p> <p><img src="trees.jpg" /></p> </body> </html>
To enable static file serving, add this code to your