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 PHP 7 client to Cloud Storage, for storing and retrieving data with Cloud Storage in an App Engine app.
The PHP 7 client also provides an API-backed stream wrapper which can be used to interact with Cloud Storage as a specialized file system.
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 Google Cloud services
You also have the option of using Cloud CDN or other Google Cloud storage services.
Serving files directly from your app
Configuring your static file handlers
To configure your app to serve the
./public directory from the
you define a handler in your
The following demonstrates how to serve the static files of a sample
./public directory. The template for this app's
instructs the browser to load the
main.css file, for example:
<link type="text/css" rel="stylesheet" href="/static/css/main.css">
./public directory is defined in the
static_dir element of the project's
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
handlers section in the above example handles three URL patterns:
The /favicon.ico handler maps a request specifically for
/favicon.icoto a file named
favicon.icoin 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
./publicdirectory. 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
the pattern for your static files should be defined before the
For more information, see the
Serving files for local development
Use PHP's built-in webserver to serve static files locally:
- Navigate to your project directory.
Start your local webserver with the following command:
php -S localhost:8080
Target your locally running PHP application with a request for static files using the
curlHTTP request utility:
curl -O http://localhost:8080/static/file/path/image.png
The request above retrieves the
Many web frameworks include support for serving static files, which you can use to serve files locally during development instead of a local webserver. For example, see the framework documentation for Symfony's Serving Files or Laravel's File Responses.
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.