Stay organized with collections Save and categorize content based on your preferences.
Applications often need to serve static files such as JavaScript, images, and CSS in addition to handling dynamic requests. Apps in the standard environment can serve static files from a Google Cloud option like Cloud Storage, serve them directly, or use a third-party content delivery network (CDN).
Hosting your static site on Google Cloud can cost less than using a traditional hosting provider, as Google Cloud provides a
free tier.
Serving files from Cloud StorageCloud Storage can host static assets for dynamic web apps. The benefits of using Cloud Storage instead of serving directly from your app include:
You can upload your assets to Cloud Storage by using the Google Cloud CLI or the Cloud Storage API.
The Google Cloud Client Library provides an idiomatic client to Cloud Storage, for storing and retrieving data with Cloud Storage in an App Engine app.
Example of serving from a Cloud Storage bucketThis example creates a Cloud Storage bucket and uploads static assets using the gcloud CLI:
Create a bucket. It's common, but not required, to name your bucket after your project ID. The bucket name must be globally unique.
gcloud storage buckets create gs://<var>your-bucket-name</var>
Set the IAM policy to grant public read access to items in the bucket.
gcloud storage buckets add-iam-policy-binding gs://<var>your-bucket-name</var> --member=allUsers --role=roles/storage.objectViewer
Upload items to the bucket. The rsync
command is typically the fastest and easiest way to upload and update assets. You could also use cp
.
gcloud storage rsync ./static gs://<var>your-bucket-name</var>/static --recursive
You can now access your static assets via https://storage.googleapis.com/<var>your-bucket-name</var>/static/...
.
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 Google Cloud servicesYou also have the option of using Cloud CDN or other Google Cloud storage services.
Serving files directly from your appTo serve static files in the standard environment, you define the handlers in your app.yaml
file using either the static_dir
or static_files
elements.
The content in the static files or static directories are unaffected by the scaling settings in your app.yaml
file. Requests to static files or static directories are handled by the App Engine infrastructure directly, and do not reach the language runtime of the application.
To configure your app to serve the ./public
directory from the /static
URL, you define a handler in your app.yaml
file.
The following demonstrates how to serve the static files of a sample app's ./public
directory. The template for this app's index.html
page instructs the browser to load the main.css
file, for example:
<link type="text/css" rel="stylesheet" href="/static/css/main.css">
The ./public
directory is defined in the static_dir
element of the project's app.yaml
file:
handlers: - url: /favicon\.ico static_files: favicon.ico upload: favicon\.ico - url: /static static_dir: public - url: /.* secure: always redirect_http_response_code: 301 script: auto
The handlers
section in the above example handles three URL patterns:
The /favicon.ico handler maps a request specifically for /favicon.ico
to a file named favicon.ico
in the app's root directory.
The /static handler maps requests for URLs that start with /static
. When App Engine receives a request for a URL beginning with /static
, it maps the remainder of the path to files in the ./public
directory. If an appropriate file is found in the directory, the contents of that file are returned to the client.
The /.* handler matches all other URLs and directs them to your app.
URL path patterns are tested in the order they appear in app.yaml
, therefore the pattern for your static files should be defined before the /.*
pattern. For more information, see the app.yaml
reference.
You can use any external third-party CDN to serve your static files and cache dynamic requests but your app might experience increased latency and cost.
For improved performance, you should use a third-party CDN that supports CDN Interconnect.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["Apps in the standard environment can serve static files directly, use a third-party content delivery network (CDN), or leverage Google Cloud options like Cloud Storage."],["Using Cloud Storage to host static assets can reduce app load, lower bandwidth charges, and function as a content delivery network by default with publicly readable objects being cached globally."],["Static files can be served directly from an app by configuring handlers in the `app.yaml` file using `static_dir` or `static_files` elements, which are managed directly by the App Engine infrastructure."],["The `app.yaml` file allows you to define handlers for specific URL patterns, such as mapping requests for `/static` to files within a designated directory like `./public`."],["While third-party CDNs can be utilized, they might result in increased latency and cost, but using a CDN that supports CDN Interconnect can improve performance."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4