Host a static website (original) (raw)

Skip to main content

Host a static website

This page 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.

Overview

Because Cloud Storage doesn't support custom domains with HTTPS on its own, this tutorial uses Cloud Storage with an external Application Load Balancer to serve content from a custom domain over HTTPS. For more ways to serve content from a custom domain over HTTPS, see troubleshooting for HTTPS serving. You can also use Cloud Storage to serve custom domain content over HTTP, which doesn't require a load balancer.

For examples and tips on static web pages, including how to host static assets for a dynamic website, see the Static Website page.

The instructions in this page describe how to perform the following steps:

Pricing

The instructions in this page use the following billable components of Google Cloud:

See the Monitoring your charges tip for details on what charges may be incurred when hosting a static website.

Limitations

You can host a static website using a bucket whose objects are readable to the public. You cannot host a static website using a bucket that haspublic access prevention enabled. To host a static website using Cloud Storage, you can use either of the following methods:

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.

  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.create permission. Learn how to grant roles.
      Go to project selector
  3. Verify that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.create permission. Learn how to grant roles.
      Go to project selector
  5. Verify that billing is enabled for your Google Cloud project.

  6. Enable theCompute Engine API for your project.

  7. Have the following Identity and Access Management roles:Storage Admin andCompute Network Admin.

  8. 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 asCloud Domains.
    This tutorial uses the domain example.com.

  9. Have a few website files you want to serve. This tutorial works best if you have at least an index page (index.html) and a 404 page (404.html).

  10. Have a Cloud Storage bucket for storing the files you want to serve. If you don't currently have a bucket,create a bucket.

  11. (Optional) If you want your Cloud Storage bucket to have the same name as your domain, you mustverify 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. If you purchased your domain through Cloud Domains, verification is automatic.

Upload your site's files

Add the files you want your website to serve to the bucket:

Console

  1. In the Google Cloud console, go to the Cloud Storage Buckets page.
    Go to Buckets
  2. In the list of buckets, click the name of the bucket that you created.
    The Bucket details page opens with the Objects tab selected.
  3. Click the Upload files button.
  4. In the file dialog, browse to the desired file and select it.

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

To learn how to get detailed error information about failed Cloud Storage operations in the Google Cloud console, seeTroubleshooting.

Command line

Use the gcloud storage cp command to copy files to your bucket. For example, to copy the file index.html from its current locationDesktop to the bucket my-static-assets:

gcloud storage cp Desktop/index.html gs://my-static-assets

If successful, the response looks like the following example:

Completed files 1/1 | 164.3kiB/164.3kiB

Client libraries

C++

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

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

C#

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

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

Go

For more information, see theCloud Storage Go API reference documentation.

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

Java

For more information, see theCloud Storage Java API reference documentation.

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

The following sample uploads an individual object:

The following sample uploads multiple objects concurrently:

The following sample uploads all objects with a common prefix concurrently:

Node.js

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

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

The following sample uploads an individual object:

The following sample uploads multiple objects concurrently:

The following sample uploads all objects with a common prefix concurrently:

PHP

For more information, see theCloud Storage PHP API reference documentation.

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

Python

For more information, see theCloud Storage Python API reference documentation.

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

The following sample uploads an individual object:

The following sample uploads multiple objects concurrently:

The following sample uploads all objects with a common prefix concurrently:

Ruby

For more information, see theCloud Storage Ruby API reference documentation.

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

Terraform

REST APIs

JSON API

  1. Have gcloud CLI installed and initialized, which lets you generate an access token for the Authorization header.
  2. Use cURL to call the JSON API with aPOST Object request. For the file index.html uploaded to a bucket named my-static-assets:
    curl -X POST --data-binary @index.html \
    -H "Content-Type: text/html" \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://storage.googleapis.com/upload/storage/v1/b/my-static-assets/o?uploadType=media&name=index.html"

XML API

  1. Have gcloud CLI installed and initialized, which lets you generate an access token for the Authorization header.
  2. Use cURL to call the XML API with aPUT Object request. For the file index.html uploaded to a bucket named my-static-assets:
    curl -X PUT --data-binary @index.html \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: text/html" \
    "https://storage.googleapis.com/my-static-assets/index.html"

Share your files

To make all objects in your bucket readable to anyone on the public internet:

Console

  1. In the Google Cloud console, go to the Cloud Storage Buckets page.
    Go to Buckets
  2. In the list of buckets, click the name of the bucket that you want to make public.
  3. Select the Permissions tab near the top of the page.
  4. If the Public access pane reads Not public, click the button labeled Remove public access prevention and click Confirm in the dialog that appears.
  5. Click the Grant access button.
    The Add principals dialog appears.
  6. In the New principals field, enter allUsers.
  7. In the Select a role drop-down, select the Cloud Storagesub-menu, and click the Storage Object Viewer option.
  8. Click Save.
  9. Click Allow public access.

Once shared publicly, a link icon appears for each object in the public access column. You can click this icon to get the URL for the object.

To learn how to get detailed error information about failed Cloud Storage operations in the Google Cloud console, seeTroubleshooting.

Command line

Use the buckets add-iam-policy-binding command:

gcloud storage buckets add-iam-policy-binding gs://my-static-assets --member=allUsers --role=roles/storage.objectViewer

Client libraries

C++

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

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

C#

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

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

Go

For more information, see theCloud Storage Go API reference documentation.

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

Java

For more information, see theCloud Storage Java API reference documentation.

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

Node.js

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

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

PHP

For more information, see theCloud Storage PHP API reference documentation.

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

Python

For more information, see theCloud Storage Python API reference documentation.

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

Ruby

For more information, see theCloud Storage Ruby API reference documentation.

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

Terraform

REST APIs

If wanted, you can alternativelymake portions of your bucket publicly accessible.

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.

You can assign an index page suffix and a custom error page, which are known as specialty pages. Assigning either is optional, but if you don't assign an index page suffix and upload the corresponding index page, users who access your top-level site are served an XML document tree containing a list of the public objects in your bucket.

For more information on the behavior of specialty pages, see Specialty pages.

Console

  1. In the Google Cloud console, go to the Cloud Storage Buckets page.
    Go to Buckets
  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.
  4. In the website configuration dialog, specify the main page and error page.
  5. Click Save.

To learn how to get detailed error information about failed Cloud Storage operations in the Google Cloud console, seeTroubleshooting.

Command line

Use the buckets update command with the --web-main-page-suffixand --web-error-page flags.

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

gcloud storage buckets update gs://my-static-assets --web-main-page-suffix=index.html --web-error-page=404.html

If successful, the command returns:

Updating gs://www.example.com/... Completed 1

Client libraries

C++

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

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

C#

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

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

Go

For more information, see theCloud Storage Go API reference documentation.

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

Java

For more information, see theCloud Storage Java API reference documentation.

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

Node.js

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

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

PHP

For more information, see theCloud Storage PHP API reference documentation.

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

Python

For more information, see theCloud Storage Python API reference documentation.

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

Ruby

For more information, see theCloud Storage Ruby API reference documentation.

To authenticate to Cloud Storage, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

REST APIs

JSON API

  1. Have gcloud CLI installed and initialized, which lets you generate an access token for the Authorization header.
  2. Create a JSON file that sets the mainPageSuffix andnotFoundPage properties in a website object to the desired pages.
    In the following sample, the mainPageSuffix is set to index.htmland notFoundPage is set to 404.html:
    {
    "website":{
    "mainPageSuffix": "index.html",
    "notFoundPage": "404.html"
    }
    }
  3. Use cURL to call the JSON API with aPATCH Bucket request. For the bucket my-static-assets:
    curl -X PATCH --data-binary @web-config.json \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://storage.googleapis.com/storage/v1/b/my-static-assets"

XML API

  1. Have gcloud CLI installed and initialized, which lets you generate an access token for the Authorization header.
  2. Create an XML file that sets the MainPageSuffix and NotFoundPageelements in a WebsiteConfiguration element to the desired pages.
    In the following sample, the MainPageSuffix is set to index.htmland NotFoundPage is set to 404.html: index.html 404.html
  3. Use cURL to call the XML API with aPUT Bucket request and websiteConfig query string parameter. For my-static-assets:
    curl -X PUT --data-binary @web-config.xml \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    https://storage.googleapis.com/my-static-assets?websiteConfig

Set up your load balancer and SSL certificate

Cloud Storage doesn't support custom domains with HTTPS on its own, so you also need to set up an SSL certificate attached to an HTTPS load balancerto serve your website through HTTPS. This section shows you how to add your bucket to a load balancer's backend and how to add a newGoogle-managed SSL certificate to the load balancer's frontend.

Select the load balancer type

  1. In the Google Cloud console, go to the Load balancing page.
    Go to Load balancing
  2. Click Create load balancer.
  3. For Type of load balancer, select Application Load Balancer (HTTP/HTTPS) and click Next.
  4. Click Configure.

The configuration window for your load balancer appears.

Basic configuration

Before continuing with the configuration, enter aLoad balancer name, such as example-lb.

Configure the frontend

This section shows you how to configure the HTTPS protocol and create an SSL certificate. You can also select an existing certificate or upload aself-managed SSL certificate.

  1. Click Frontend configuration.
  2. (Optional) Give your frontend configuration a Name.
  3. For Protocol, select HTTPS (includes HTTP/2).
  4. For IP version, select IPv4. If you prefer IPv6, seeIPv6 termination for additional information.
  5. For the IP address field:
    • In the drop-down, click Create IP address.
    • In the Reserve a new static IP address dialog, enter a name, such as example-ip for the Name of the IP address.
    • Click Reserve.
  6. For Port, select 443.
  7. In the Certificate field dropdown, select Create a new certificate. The certificate creation form appears in a panel. Configure the following:
    • Give your certificate a Name, such as example-ssl.
    • For Create mode, select Create Google-managed certificate.
    • For Domains, enter your website name, such as www.example.com. If you want to serve your content through additional domains such as the root domain example.com, press Enter to add them on additional lines. Each certificate has a limit of 100 domains.
  8. Click Create.
  9. (Optional) If you want Google Cloud to automatically set up apartial HTTP load balancer for redirecting HTTP traffic, select the checkbox next to Enable HTTP to HTTPS redirect.
  10. Click Done.

Configure the backend

  1. Click Backend configuration.
  2. In the Backend services & backend buckets dropdown, click Create a backend bucket.
  3. Choose a Backend bucket name, such as example-bucket. The name you choose can be different from the name of the bucket you created earlier.
  4. Click Browse, found in the Cloud Storage bucket field.
  5. Select the my-static-assets bucket you created earlier, and clickSelect.
  6. (Optional) If you want to use Cloud CDN, select the checkbox forEnable Cloud CDN and configure Cloud CDN as desired. Note that Cloud CDN may incur additional costs.
  7. Click Create.

Configure routing rules

Routing rules are the components of an external Application Load Balancer's URL map. For this tutorial, you should skip this portion of the load balancer configuration, because it is automatically set to use the backend you just configured.

Review the configuration

  1. Click Review and finalize.
  2. Review the Frontend, Routing rules, and Backend.
  3. Click Create.

You may need to wait a few minutes for the load balancer to be created.

Connect your domain to your load balancer

After the load balancer is created, click the name of your load balancer:example-lb. Note the IP address associated with the load balancer: for example, 30.90.80.100. To point your domain to your load balancer, create anA record using your domain registration service. If you added multiple domains to your SSL certificate, you must add an A record for each one, all pointing to the load balancer's IP address. For example, to create A records forwww.example.com and example.com:

NAME TYPE DATA www A 30.90.80.100 @ A 30.90.80.100

See Troubleshooting domain status for more information about connecting your domain to your load balancer.

It might take up to 60-90 minutes for Google Cloud to provision the certificate and make the site available through the load balancer. To monitor the status of your certificate:

Console

  1. Go to the Load balancing page in the Google Cloud console.
    Go to Load balancing
  2. Click the name of your load balancer: example-lb.
  3. Click the name of the SSL certificate associated with the load balancer:example-ssl.
  4. The Status and Domain status rows show the certificate status. Both must be active in order for the certificate to be valid for your website.

Command line

  1. To check the certificate status, run the following command:
    gcloud compute ssl-certificates describe CERTIFICATE_NAME \
    --global \
    --format="get(name,managed.status)"
  2. To check the domain status, run the following command:
    gcloud compute ssl-certificates describe CERTIFICATE_NAME \
    --global \
    --format="get(managed.domainStatus)"

See Troubleshooting SSL certificates for more information about certificate status.

Test the website

Once the SSL certificate is active, verify that content is served from the bucket by going to https://www.example.com/test.html, where test.html is an object stored in the bucket that you're using as the backend. If you set theMainPageSuffix property, https://www.example.com goes to index.html.

Clean up

After you finish the tutorial, you can clean up the resources that you created so that they stop using quota and incurring charges. The following sections describe how to delete or turn off these resources.

Delete 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 Google Cloud console, go to the Manage resources page.
    Go to Manage resources
  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then clickShut down to delete the project.

Delete the load balancer and bucket

If you don't want to delete the entire project, delete the load balancer and bucket that you created for the tutorial:

  1. Go to the Load balancing page in the Google Cloud console.
    Go to Load balancing
  2. Select the checkbox next to example-lb.
  3. Click Delete.
  4. (Optional) Select the checkbox next to the resources you want to delete along with the load balancer, such as the my-static-assets bucket or theexample-ssl SSL certificate.
  5. Click Delete load balancer or Delete load balancer and the selected resources.

Release a reserved IP address

To delete the reserved IP address you used for the tutorial:

  1. In the Google Cloud console, go to the External IP addresses page.
    Go to External IP addresses
  2. Select the checkboxes next to example-ip.
  3. Click Release static address.
  4. In the confirmation window, click Delete.

What's next

Try it for yourself

If you're new to Google Cloud, create an account to evaluate how Cloud Storage performs in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.

Try Cloud Storage free

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-12-09 UTC.