This tutorial describes how to configure a Cloud Storage bucket to host a static website. Static web pages can contain client-side technologies such as HTML, CSS, and JavaScript. For more information on static web pages, such as examples, tips and troubleshooting, 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.
- 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:
- Hosting a Static Website
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
-
Sign in to your Google account.
If you don't already have one, sign up for a new account.
- Select or create a Cloud Platform project.
- Enable billing for your project.
- 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. - Verify that you own or manage the domain you will be using. To demonstrate
that you are an owner or manager of a domain, use one of the
Google Webmaster Central
verification processes. Make sure you are verifying the domain, for example,
example.com, and not a site on the domain, such aswww.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.
For more information, see Domain-Named Bucket Verification.
Creating a CNAME alias
Create a CNAME alias that points to c.storage.googleapis.com.
A CNAME alias is a DNS record that lets you use a URL from your own
domain to access resources, such as a bucket and objects, in
Cloud Storage using your custom domain URL. For example, for
www.example.com, the CNAME record might contain the following information:
NAME TYPE DATA www.example.com CNAME c.storage.googleapis.com
Your domain registration service should have a way that you can administer
your domain, including adding a CNAME resource record. For example, if you use
Google Domains, instructions for adding a resource record can be found on the
Google Domains Help page, in the Resource records drop-down section.
For more information about CNAME redirects, see URI for CNAME aliasing.
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
- Open the Cloud Storage browser in the Google Cloud Platform Console.
Open the Cloud Storage browser - Click Create bucket.
-
In the Create bucket dialog, specify:
- The Name of your bucket.
- The Storage class for your bucket: Multi-Regional.
- The Location of your bucket: United States.

- Click Create.
If successful, you are taken to the bucket's page with the text "There are no 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/...
Client Libraries
C#
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
Go
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
Java
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
Node.js
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
PHP
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
Python
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
Ruby
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
REST APIS
JSON API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
- Create a .json file that assigns the
nameproperty your website name: - Use
cURLto 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://www.googleapis.com/storage/v1/b?project=my-static-website"
{
"name": "www.example.com"
}
XML API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
- Use
cURLto 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
- Open the Cloud Storage browser in the Google Cloud Platform Console.
Open the Cloud Storage browser - In the list of buckets, click on the bucket you created.
- Click Upload files.
- In the file dialog, browse to the desired file and select it.
After the upload completes, you should see the file name, size, type, and last modified date in the bucket.
For example a bucket with two files index.html and
404.html appears in the Cloud Platform Console as:
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
Client Libraries
C#
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
Go
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
Java
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
Node.js
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
PHP
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
Python
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
Ruby
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
REST APIS
JSON API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
- Add the file data to the request body.
- Use
cURLto call the JSON API with aPOSTObject 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://www.googleapis.com/upload/storage/v1/b/www.example.com/o?uploadType=media&name=index.html"
See Performing a Simple Upload for more information about uploading objects with the JSON API.
XML API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
- Add the object's data to the request body.
- Use
cURLto call the XML API with aPUTObject 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"
Sharing your files
To share publicly the files that you want to serve:
Console
- Open the Cloud Storage browser in the Google Cloud Platform Console.
Open the Cloud Storage browser - In the list of buckets, click on the bucket that contains the objects you uploaded.
- For each object you want to share publicly, click the checkbox in the Share publicly column.
If successful, a link titled Public link appears next to the checkbox.
gsutil
Use the gsutil acl ch command to grant all users read
permission for each of the files stored in your bucket that you plan to serve.
For example:
gsutil acl ch -u AllUsers:R gs://www.example.com/index.html
If successful, the command returns:
Updated ACL on gs://www.example.com/index.html
Client Libraries
C#
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
Go
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
Java
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
Node.js
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
PHP
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
Python
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
Ruby
For more on installing and creating a Cloud Storage client, refer to Cloud Storage Client Libraries.
REST APIS
JSON API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
- Create a .json file that contains the following information:
- Use
cURLto call the JSON API with an Insert ACL request. For the index page of www.example.com:curl -X POST --data-binary @public-read.json \ -H "Authorization: Bearer ya29.AHES6ZRVmB7fkLtd1XTmq6mo0S1wqZZi3-Lh_s-6Uw7p8vtgSwg" \ -H "Content-type: application/json" \ "https://www.googleapis.com/storage/v1/b/www.example.com/o/index.html/acl"
{
"entity": "allUsers",
"role": "READER"
}
XML API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
- Create a .xml file that contains the following information:
- Use
cURLto call the XML API with an Set Object ACL request. For the index page of www.example.com:curl -X PUT --data-binary @[XML_FILE_NAME].xml \ -H "Authorization: Bearer ya29.AHES6ZRVmB7fkLtd1XTmq6mo0S1wqZZi3-Lh_s-6Uw7p8vtgSwg" \ "https://storage.googleapis.com/www.example.com/index.html?acl"
<AccessControlList>
<Entries>
<Entry>
<Scope type="AllUsers"/>
<Permission>READ</Permission>
</Entry>
</Entries>
</AccessControlList>
Optional: Assigning pages
From the files you shared publicly, you can assign an index page suffix and a custom error page.
An index page (also called a webserver directory index)
is the file returned to visitors to your static site who request a "directory".
If the top level site, such as http://www.example.com, is requested, or a
missing page is requested, e.g., http://www.example.com/directory, the
service attempts to return an index page. If you haven't assigned an index
page suffix, or if the "directory" doesn't have an associated index page,
Cloud Storage returns an error page. The
property that controls which file acts as the index page is MainPageSuffix.
For example, if the MainPageSuffix is index.html, Cloud Storage attempts
to return the object www.example.com/index.html when a user requests
http://www.example.com. Similarly, if there is no object
www.example.com/directory, then Cloud Storage attempts to return the
object www.example.com/directory/index.html when a user requests
http://www.example.com/directory. If there is no such object, Cloud Storage
returns an error page.
The error page is the file returned to visitors of your static site who
request a URL that does not correspond to either an existing file, or, if an
index page suffix is set, the appropriate index object. 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.
In the following sample, an index page suffix is set to index.html and a
custom error page is set to 404.html:
Console
- Open the Cloud Storage browser in the Google Cloud Platform Console.
Open the Cloud Storage browser - In the list of buckets, find the bucket you created.
- Click the more actions icon
next to the bucket
and select
Edit website configuration.
- In the Configure website dialog, specify the Main Page and the 404 (Not Found) Page.
- Click Save.
gsutil
Using the gsutil tool:
The following command sets the MainPageSuffix property to
index.html and the NotFoundPage property to 404.html
for the bucket www.example.com.
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/...
REST APIS
JSON API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
- Create a .json file that sets the
mainPageSuffixandnotFoundPageproperties in awebsiteobject to the desired pages:{ "website":{ "mainPageSuffix": "index.html", "notFoundPage": "404.html" } } - Use
cURLto call the JSON API with aPATCHBucket 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://www.googleapis.com/storage/v1/b/www.example.com"
XML API
- Get an authorization access token from the OAuth 2.0 Playground. Configure the playground to use your own OAuth credentials.
- Create a .xml file that sets the
MainPageSuffixandNotFoundPageelements in aWebsiteConfigurationelement to the desired pages:<WebsiteConfiguration> <MainPageSuffix>index.html</MainPageSuffix> <NotFoundPage>404.html</NotFoundPage> </WebsiteConfiguration>
- Use
cURLto call the XML API, with aPUTBucket request andwebsiteConfigquery 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
Testing the website
Test that content is served from the bucket by requesting the domain name in a browser,
either with a path to an object, or if you set the MainPageSuffix property, with just
the domain name.
Cleaning up
After you've finished the Hosting a Static Website tutorial, you can clean up the resources you created on Google Cloud Platform so 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 you created for the tutorial.
To delete the project:
- In the Cloud Platform Console, go to the Projects page.
-
In the project list, select the project you
want to delete and click Delete project.
- 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
- Open the Cloud Storage browser in the Google Cloud Platform Console.
Open the Cloud Storage browser - Select the checkbox next to the bucket that you created.
- Click Delete.
- In the overlay that appears, click Delete to permanently delete the bucket and its contents.
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
What's next
- See examples of, tips for and troubleshooting using buckets to host a static website.
- Learn about hosting static assets for a dynamic website.
- Learn about all web serving options.
- Try out other Google Cloud Platform features for yourself. Have a look at our tutorials.