Hosting a static website  |  Cloud Storage  |  Google Cloud

March 29, 2020

Hosting a static website  |  Cloud Storage  |  Google Cloud

This tutorial describes how to configure a Cloud Storage bucket to host a static website for a domain you own. Static web pages can contain client-side technologies such as HTML, CSS, and JavaScript. They cannot contain dynamic content such as server-side scripts like PHP. For more information on static web pages, such as examples and tips, see the Static Website page.

This tutorial also applies to hosting static assets for a dynamic website.

Caution: This tutorial applies to using HTTP. We recommend that you don’t serve content that contains sensitive or private data via HTTP from your Cloud Storage bucket. For options for serving content on a custom domain over HTTPS, see the related troubleshooting topic.

Objectives

In this tutorial you will:

  • Point your domain to Cloud Storage by using a CNAME record.
  • Create a bucket that is linked to your domain.
  • Upload and share your site’s files.
  • Test the website.

Costs

This tutorial uses the following billable component of Google Cloud:

  • Cloud Storage

See the Monitoring your storage charges tip for details on what charges may be incurred when hosting a static website, and see the Pricing page for details on Cloud Storage costs.

Before you begin

  1. Sign in to your Google Account.

    If you don’t already have one, sign up for a new account.

  2. In the Cloud Console, on the project selector page, select or create a Cloud project.

    Note: If you don’t plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

    Go to the project selector page

  3. Make sure that billing is enabled for your Google Cloud project. Learn how to confirm billing is enabled for your project.

  4. Have a domain that you own or manage. If you don’t have an existing domain, there are many services through which you can register a new domain, such as Google Domains.

    This tutorial uses the domain example.com.

  5. Verify that you own or manage the domain that you will be using. Make sure you are verifying the top-level domain, such as example.com, and not a subdomain, such as www.example.com.

    Note: If you own the domain you are associating to a bucket, you might have already performed this step in the past. If you purchased your domain through Google Domains, verification is automatic.

Connecting your domain to Cloud Storage

To connect your domain to Cloud Storage, create a CNAME record through your domain registration service. A CNAME record is a type of DNS record. It directs traffic that requests a URL from your domain to the resources you want to serve, in this case objects in your Cloud Storage buckets. For www.example.com, the CNAME record might contain the following information:

NAME TYPE DATA www.example.com CNAME c.storage.googleapis.com.

For more information about CNAME redirects, see URI for CNAME aliasing.

To connect your domain to Cloud Storage:

  1. Create a CNAME record that points to c.storage.googleapis.com..

    Your domain registration service should have a way for you to administer your domain, including adding a CNAME record. For example, if you use Google Domains, instructions for adding a CNAME record can be found on the Google Domains Help page.

    Note: DNS does not support creating a CNAME record on a root domain, such as example.com, so in most cases, you must create your CNAME record on a subdomain, such as www.example.com or myblog.example.com. Some DNS providers offer a non-standard feature that allows a root domain to be aliased to another name without the use of CNAME. Consult your DNS provider documentation to determine if such a feature is available.

Creating a bucket

Create a bucket whose name matches the CNAME you created for your domain.

For example, if you added a CNAME record pointing www.example.com to c.storage.googleapis.com., then create a bucket with the name “www.example.com”.

To create a bucket:

Console

  1. Open the Cloud Storage browser in the Google Cloud Console.
    Open the Cloud Storage browser
  2. Click Create bucket to open the bucket creation form.

  3. Enter your bucket information and click Continue to complete each step:

    • The Name of your bucket, which matches the CNAME you created for your domain.

    • The Storage class and Location for your bucket.

    • The Access control model for your bucket.

  4. Click Create.

If successful, you are taken to the bucket’s page with the text “There are no live objects in this bucket.”

gsutil

Use the gsutil mb command:

gsutil mb gs://www.example.com

If successful, the command returns:

Creating gs://www.example.com/…

Code samples

C++

For more information, see the Cloud Storage C++ API reference documentation.

C#

For more information, see the Cloud Storage C# API reference documentation.

Go

For more information, see the Cloud Storage Go API reference documentation.

Java

For more information, see the Cloud Storage Java API reference documentation.

Node.js

For more information, see the Cloud Storage Node.js API reference documentation.

PHP

For more information, see the Cloud Storage PHP API reference documentation.

Python

For more information, see the Cloud Storage Python API reference documentation.

Ruby

For more information, see the Cloud Storage Ruby API reference documentation.

REST APIs

JSON API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Create a .json file that assigns your website name to the name property:

    { “name”: “www.example.com” }

  3. Use cURL to call the JSON API. For www.example.com:

    curl -X POST –data-binary @website-bucket-name.json -H “Authorization: Bearer ya29.AHES6ZRVmB7fkLtd1XTmq6mo0S1wqZZi3-Lh_s-6Uw7p8vtgSwg” -H “Content-Type: application/json” “https://storage.googleapis.com/storage/v1/b?project=my-static-website”

XML API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Use cURL to call the XML API to create a bucket with your website name. For www.example.com:

    curl -X PUT -H “Authorization: Bearer ya29.AHES6ZRVmB7fkLtd1XTmq6mo0S1wqZZi3-Lh_s-6Uw7p8vtgSwg” -H “x-goog-project-id: my-static-website” “https://storage.googleapis.com/www.example.com”

Uploading your site’s files

To add to your bucket the files you want your website to serve:

Console

  1. Open the Cloud Storage browser in the Google Cloud Console.
    Open the Cloud Storage browser
  2. In the list of buckets, click on the name of the bucket that you created.

  3. Click the Upload files button in the Objects tab.

  4. In the file dialog, browse to the desired file and select it.

After the upload completes, you should see the file name along with file information displayed in the bucket.

gsutil

Use the gsutil cp command to copy files to your bucket. For example, to copy the file index.html from its current location Desktop:

gsutil cp Desktop/index.html gs://www.example.com

If successful, the command returns:

Copying file://Desktop/index.html [Content-Type=text/html]… Uploading gs://www.example.com/index.html: 0 B/2.58 KiB Uploading gs://www.example.com/index.html: 2.58 KiB/2.58 KiB

Code samples

For more information, see the Cloud Storage C++ API reference documentation.

For more information, see the Cloud Storage C# API reference documentation.

For more information, see the Cloud Storage Go API reference documentation.

For more information, see the Cloud Storage Java API reference documentation.

For more information, see the Cloud Storage Node.js API reference documentation.

For more information, see the Cloud Storage PHP API reference documentation.

For more information, see the Cloud Storage Python API reference documentation.

Use theBlob.upload_from_file(),Blob.upload_from_filename(), orBlob.upload_from_string()method to upload an object.

For more information, see the Cloud Storage Ruby API reference documentation.

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Use cURL to call the JSON API with a POST Object request. For the index page of www.example.com:

    curl -X POST –data-binary @index.html -H “Content-Type: text/html” -H “Authorization: Bearer ya29.AHES6ZRVmB7fkLtd1XTmq6mo0S1wqZZi3-Lh_s-6Uw7p8vtgSwg” “https://storage.googleapis.com/upload/storage/v1/b/www.example.com/o?uploadType=media&name=index.html”

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Use cURL to call the XML API with a PUT Object request. For the index page of www.example.com:

    curl -X PUT –data-binary @index.html -H “Authorization: Bearer ya29.AHES6ZRVmB7fkLtd1XTmq6mo0S1wqZZi3-Lh_s-6Uw7p8vtgSwg” -H “Content-Type: text/html” “https://storage.googleapis.com/www.example.com/index.html”

Note: If you are migrating an existing website, or copying a large number of files, use gsutil rsync to move your content. You can use the -R option to recursively copy directory trees. For example, to synchronize a local directory named local-dir with a bucket, use: gsutil rsync -R local-dir gs://www.example.com

Sharing your files

By using the special identifier allUsers, you can give anyone on the Internet access to your files.

  • To make all files in your bucket publicly accessible, follow the steps in making groups of objects publicly readable.

    Caution: Before making your bucket publicly accessible, make sure that the files in your bucket do not contain sensitive or private information.

  • To make individual files in your bucket publicly accessible, follow the steps in making individual objects publicly readable.

    Note: You can set the default object ACL for your bucket so that future files uploaded to your bucket are publicly accesible by default.

Generally, making all files in your bucket publicly accessible is easier and faster.

Visitors receive a http 403 response code when requesting the URL for a non-public or non-existent file. See the next section for information on how to add an error page that uses a http 404 response code.

Recommended: Assigning specialty pages

You can assign an index page suffix, which is controlled by the MainPageSuffix property and a custom error page, which is controlled by the NotFoundPage property. Assigning either is optional, but without an index page, nothing is served when users access your top-level site, for example, http://www.example.com.

Index pages

An index page (also called a webserver directory index) is a file served to visitors when they request a URL that doesn’t have an associated file. When you assign a MainPageSuffix, Cloud Storage looks for a file with that name whose prefix matches the URL the visitor requested.

For example, say you set the MainPageSuffix of your static website to index.html. Additionally, say you have no file named directory in your bucket www.example.com. In this situation, if a user requests the URL http://www.example.com/directory, Cloud Storage attempts to serve the file www.example.com/directory/index.html. If that file also doesn’t exist, Cloud Storage returns an error page.

The MainPageSuffix also controls the file served when users request the top level site. Continuing the above example, if a user requests http://www.example.com, Cloud Storage attempts to serve the file www.example.com/index.html.

For more information on the cases in which the index page is served, see Website configuration examples.

Error page

The error page is the file returned to visitors of your static site who request a URL that does not correspond to an existing file. If you have assigned a MainPageSuffix, Cloud Storage only returns the error page if there is neither a file with the requested name nor an applicable index page.

When returning an error page, the http response code is 404. The property that controls which file acts as the error page is NotFoundPage. If you don’t set NotFoundPage, users receive a generic error page.

Setting MainPageSuffix and NotFoundPage

In the following sample, the MainPageSuffix is set to index.html and NotFoundPage is set to 404.html:

Console

  1. Open the Cloud Storage browser in the Google Cloud Console.
    Open the Cloud Storage browser
  2. In the list of buckets, find the bucket you created.

  3. Click the Bucket overflow menu () associated with the bucket and select Edit website configuration.

    Note: This option is only available for buckets whose name matches a valid website pattern.

  4. In the website configuration dialog, specify the main page and error page.

  5. Click Save.

Note: View, change, or remove these settings from the Edit website configuration menu.

gsutil

Use the gsutil web set command to set the MainPageSuffix property with the -m flag and the NotFoundPage with the -e flag:

gsutil web set -m index.html -e 404.html gs://www.example.com

If successful, the command returns:

Setting website config on gs://www.example.com/…Note: Change or remove these settings with additional gsutil web set commands and view these settings with the gsutil web get command.

Code samples

REST APIs

JSON API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Create a .json file that sets the mainPageSuffix and notFoundPage properties in a website object to the desired pages:

    { “website”:{ “mainPageSuffix”: “index.html”, “notFoundPage”: “404.html” } }

  3. Use cURL to call the JSON API with a PATCH Bucket request. For www.example.com:

    curl -X PATCH –data-binary @web-config.json -H “Authorization: Bearer ya29.AHES6ZRVmB7fkLtd1XTmq6mo0S1wqZZi3-Lh_s-6Uw7p8vtgSwg” -H “Content-Type: application/json” “https://storage.googleapis.com/storage/v1/b/www.example.com”

XML API

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Create a .xml file that sets the MainPageSuffix and NotFoundPage elements in a WebsiteConfiguration element to the desired pages:

    index.html 404.html

  3. Use cURL to call the XML API with a PUT Bucket request and websiteConfig query string parameter. For www.example.com:

    curl -X PUT –data-binary @web-config.xml -H “Authorization: Bearer ya29.AHES6ZRVmB7fkLtd1XTmq6mo0S1wqZZi3-Lh_s-6Uw7p8vtgSwg” https://storage.googleapis.com/www.example.com?websiteConfig

Note: Change or remove these settings with additional PUT requests and view these settings with a GET request.

Testing the website

Verify that content is served from the bucket by requesting the domain name in a browser. You can do this with a path to an object or with just the domain name, if you set the MainPageSuffix property.

For example, if you have an object named test.html stored in a bucket named www.example.com, check that it’s accessible by going to www.example.com/test.html in your browser.

Cleaning up

After you’ve finished the Hosting a Static Website tutorial, you can clean up the resources that you created on GCP so they won’t take up quota and you won’t be billed for them in the future. The following sections describe how to delete or turn off these resources.

Deleting the project

The easiest way to eliminate billing is to delete the project that you created for the tutorial.

To delete the project:

    Caution: Deleting a project has the following effects:

    • Everything in the project is deleted. If you used an existing project for this tutorial, when you delete it, you also delete any other work you’ve done in the project.
    • Custom project IDs are lost. When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an appspot.com URL, delete selected resources inside the project instead of deleting the whole project.

    If you plan to explore multiple tutorials and quickstarts, reusing projects can help you avoid exceeding project quota limits.

  1. In the Cloud Console, go to the Manage resources page.

    Go to the Manage resources page

  2. In the project list, select the project that you want to delete and then click Delete delete.
  3. In the dialog, type the project ID and then click Shut down to delete the project.

Deleting the bucket

If you do not want to delete the entire project, delete the bucket that you used for hosting your website:

Console

  1. Open the Cloud Storage browser in the Google Cloud Console.
    Open the Cloud Storage browser
  2. Select the checkbox associated with the bucket you want to delete.

  3. Click Delete.

  4. In the overlay window that appears, confirm you want to delete the bucket and its contents by clicking Delete.

gsutil

Use the gsutil rm command with the -r flag to delete the bucket and the contents inside of it:

gsutil rm -r gs://www.example.com

The response looks like the following example:

Removing gs://www.example.com/…

Code samples

C++

For more information, see the Cloud Storage C++ API reference documentation.

C#

For more information, see the Cloud Storage C# API reference documentation.

Go

For more information, see the Cloud Storage Go API reference documentation.

Java

For more information, see the Cloud Storage Java API reference documentation.

Node.js

For more information, see the Cloud Storage Node.js API reference documentation.

PHP

For more information, see the Cloud Storage PHP API reference documentation.

Python

For more information, see the Cloud Storage Python API reference documentation.

Ruby

For more information, see the Cloud Storage Ruby API reference documentation.

REST APIs

JSON API

Note: Your bucket must be empty before you can delete it.

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Use cURL to call the JSON API with a DELETE Bucket request:

    curl -X DELETE -H “Authorization: Bearer ya29.AHES6ZRVmB7fkLtd1XTmq6mo0S1wqZZi3-Lh_s-6Uw7p8vtgSwg” “https://storage.googleapis.com/storage/v1/b/www.example.com”

If successful, the response contains a 204 status code.

XML API

Note: Your bucket must be empty before you can delete it.

  1. Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
  2. Use cURL to call the XML API with a DELETE Bucket request:

    curl -X DELETE -H “Authorization: Bearer ya29.AHES6ZRVmB7fkLtd1XTmq6mo0S1wqZZi3-Lh_s-6Uw7p8vtgSwg” “https://storage.googleapis.com/www.example.com”

What’s next

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 2020-03-27.