A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.google.com/appengine/docs/flexible/serving-static-files below:

Serving static files | Google App Engine flexible environment docs

Serving static files

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 flexible 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).

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:

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 bucket

This example creates a Cloud Storage bucket and uploads static assets using the gcloud CLI:

  1. 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>
    
  2. 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
    
  3. 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 services

You also have the option of using Cloud CDN or other Google Cloud 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:

Note: In production environments, it's generally best practice to serve your static content separately from your app, either in Google Cloud or externally using a third-party CDN. Example of serving static files with your app Go

The following sample demonstrates how to serve static files with your app. You can use the sample application in this guide for any supported version of Go by specifying the runtime version and operating system in your app.yaml file.

You can use the standard http.FileServer or http.ServeFile to serve files directly from your app.

Java

The following sample demonstrates how to serve static files with your app. You can use the sample application in this guide for any supported version of Java by specifying the runtime version and operating system in your app.yaml file.

The Java runtime's servlet container will use your app's deployment descriptor, web.xml file, to map URLs to servlets, including static assets. If you don't specify a web.xml, a default is used that maps everything to the default servlet.

In this example, ./src/main/webapp/index.html refers to a style sheet served from /stylesheets/styles.css.

The styles.css file is located at ./src/main/webapp/stylesheets/styles.css.

You can explicitly configure how static files are handled in the web.xml file. For example, if you wanted to map requests for all files that have the .jpg extension:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
</servlet-mapping>

If you are using a web framework, such as Play, you will need to refer to the framework's documentation on static assets.

Node.js

The following sample demonstrates how to serve static files with your app. You can use the sample application in this guide for any supported version of Node.js by specifying the runtime version and operating system in your app.yaml file.

Most web frameworks include support for serving static files. In this sample, the application uses the express.static middleware to serve files from the ./public directory to the /static URL.

The view refers to /static/main.css.

The style sheet itself is located at ./public/css, which is served from /static/main.css.

Other Node.js frameworks, such as Hapi, Koa, and Sails typically support serving static files directly from the application. Refer to their documentation for details on how to configure and use static content.

PHP

The PHP runtime runs nginx to serve your app, which is configured to serve static files in your project directory. You must declare the document root by specifying document_root in your app.yaml file. You can use the sample application in this guide for any supported version of PHP by specifying the runtime version and operating system in your app.yaml file.

Python

The following sample demonstrates how to serve static files with your app. You can use the sample application in this guide for any supported version of Python by specifying the runtime version and operating system in your app.yaml file.

Most web frameworks include support for serving static files. In this sample, the app uses Flask's built-in ability to serve files in ./static directory from the /static URL.

The app includes a view that renders the template. Flask automatically serves everything in the ./static directory without additional configuration.

The template rendered by the view includes a style sheet located at /static/main.css.

The style sheet is located at ./static/main.css.

Other Python frameworks, such as Django, Pyramid, and Bottle typically support serving static files directly from the app. Refer to their documentation for details on how to configure and use static content.

Ruby

Most web frameworks include support for serving static files. The following sample demonstrates how to serve static files with your app. You can use the sample application in this guide for any supported version of Ruby by specifying the runtime version and operating system in your app.yaml file.

Sinatra

The Sinatra web framework serves files from the ./public directory by default. This app includes a view that refers to /application.css.

The style sheet is located at ./public/application.css which is served from /application.css.

Ruby on Rails

The Ruby on Rails web framework serves files from the ./public directory by default. Static JavaScript and CSS files can also be generated by the Rails asset pipeline.

These example apps contain a layout view that include all the application style sheets.

The style sheet itself is a .css file located at ./public/application.css.

By default, Rails apps don't generate or serve static assets when running in production.

The Ruby runtime executes rake assets:precompile during deployment to generate static assets and sets the RAILS_SERVE_STATIC_FILES environment variable to enable static file serving in production.

.NET

The following sample demonstrates how to serve static files with your app. You can use the sample application in this guide for any supported version of .NET by specifying the runtime version and operating system in your app.yaml file.

To enable static file serving, add:

Serving from a third-party content delivery network

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."],[[["Cloud Storage can host static assets for web applications, acting as a content delivery network (CDN) which can reduce app load and bandwidth costs."],["Static files can be served directly from your application, but this can strain resources and increase latency compared to using a dedicated storage service."],["Serving static content separately from your application using services like Google Cloud or a third-party CDN is recommended in production environments."],["Various programming languages and frameworks (like Go, Java, Node.js, PHP, Python, Ruby, and .NET) provide different methods and configurations for serving static files, often relying on built-in middleware or specific configurations."],["Utilizing a third-party CDN that supports CDN Interconnect can further improve performance when serving static files, but might result in added latency or costs."]]],[]]


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