web - Set a main page and/or error page for one or more buckets

web - Set a main page and/or error page for one or more buckets

Synopsis

gsutil web set [-m main_page_suffix] [-e error_page] bucket_url...
gsutil web get bucket_url

Description

The Website Configuration feature enables you to configure a Google Cloud Storage bucket to behave like a static website. This means requests made via a domain-named bucket aliased using a Domain Name System "CNAME" to c.storage.googleapis.com will work like any other website, i.e., a GET to the bucket will serve the configured "main" page instead of the usual bucket listing and a GET for a non-existent object will serve the configured error page.

For example, suppose your company's Domain name is example.com. You could set up a website bucket as follows:

  1. Create a bucket called example.com (see the "DOMAIN NAMED BUCKETS" section of "gsutil help naming" for details about creating such buckets).

  2. Create index.html and 404.html files and upload them to the bucket.

  3. Configure the bucket to have website behavior using the command:

    gsutil web set -m index.html -e 404.html gs://www.example.com
    
  4. Add a DNS CNAME record for example.com pointing to c.storage.googleapis.com (ask your DNS administrator for help with this).

Now if you open a browser and navigate to http://www.example.com, it will display the main page instead of the default bucket listing. Note: It can take time for DNS updates to propagate because of caching used by the DNS, so it may take up to a day for the domain-named bucket website to work after you create the CNAME DNS record.

Additional notes:

  1. Because the main page is only served when a bucket listing request is made via the CNAME alias, you can continue to use "gsutil ls" to list the bucket and get the normal bucket listing (rather than the main page).
  2. The main_page_suffix applies to each subdirectory of the bucket. For example, with the main_page_suffix configured to be index.html, a GET request for http://www.example.com would retrieve http://www.example.com/index.html, and a GET request for http://www.example.com/photos would retrieve http://www.example.com/photos/index.html.
  3. There is just one 404.html page: For example, a GET request for http://www.example.com/photos/missing would retrieve http://www.example.com/404.html, not http://www.example.com/photos/404.html.
  4. For additional details see https://cloud.google.com/storage/docs/website-configuration.

The web command has two sub-commands:

Set

The "gsutil web set" command will allow you to configure or disable Website Configuration on your bucket(s). The "set" sub-command has the following options (leave both options blank to disable):

Set Options

-m <index.html>
 Specifies the object name to serve when a bucket listing is requested via the CNAME alias to c.storage.googleapis.com.
-e <404.html> Specifies the error page to serve when a request is made for a non-existent object via the CNAME alias to c.storage.googleapis.com.

Get

The "gsutil web get" command will gets the web semantics configuration for a bucket and displays a JSON representation of the configuration.

In Google Cloud Storage, this would look like:

{
  "notFoundPage": "404.html",
  "mainPageSuffix": "index.html"
}

Send feedback about...

Cloud Storage Documentation