How to use Google Cloud Storage to host a website

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.

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 Cloud Platform:

  • 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. Select or create a GCP project.

    GO TO THE PROJECT SELECTOR PAGE

  3. Make sure that billing is enabled for your Google Cloud Platform project.

    LEARN HOW TO ENABLE BILLING

  4. Have a domain that you own or manage. If you don’t have an existing domain, there are many services through which can 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.

Creating a CNAME record

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.

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:

  1. Open the Cloud Storage browser in the Google Cloud Platform 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:
  4. Click Create.

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

Uploading your site’s files

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

  1. Open the Cloud Storage browser in the Google Cloud Platform 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.

Sharing your files

You can either make all files in your bucket publicly accessible, or you can set individual objects to be accessible through your website. Generally, making all files in your bucket accessible is easier and faster.

If you choose to control the accessibility of individual files, you can set the default object ACL for your bucket so that subsequent files uploaded to your bucket are shared by default.

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 URLhttp://www.example.com/directory, Cloud Storage attempts to serve the filewww.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 filewww.example.com/index.html.

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:

  1. Open the Cloud Storage browser in the Google Cloud Platform Console.
    OPEN THE CLOUD STORAGE BROWSER
  2. In the list of buckets, find the bucket you created.
  3. Click the more actions icon More actions icon. next to the bucket and select Edit website configuration.
  4. In the Configure website dialog, specify the Main Page and the 404 (Not Found) Page.
  5. Click Save.

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:

  1. In the GCP Console, go to the Projects page.GO TO THE PROJECTS PAGE
  2. In the project list, select the project you want to delete and click 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:

  1. Open the Cloud Storage browser in the Google Cloud Platform Console.
    OPEN THE CLOUD STORAGE BROWSER
  2. Select the checkbox of 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.