Saturday, October 15, 2016

Hosting a Static Website on Google Cloud Storage

I have already hosted a static website by Google Cloud Storage which is provided by Google Cloud Platform. We can manage the Google Cloud Storage using gsutil command which is related in Google Cloud SDK.

Prerequisite

gcloud

If you haven't logged-in to auth, type the following lines;
$ gcloud init
$ gcloud auth login
Set a project by project id if you have already authenticated to Google Cloud Platform.
# Confirm your configuration of gcloud.
$ gcloud config list
Your active configuration is: [default]

[core]
account = [your-account]
disable_usage_reporting = True
project = [your-project-id]

$ gcloud config set project [another-project-id]

gsutil command

Installation

See Google Cloud SDK Documentation

Transfer files using gsutil rsync

Synchronous remote directory with local one with using gsutil rsync. See the following command;
$ gsutil -m rsync -d -r [local-dir] gs://[bucket-name]/[dir-if-needed]
The option of -m will execute multi process. The options of -d and -r are the same of the original rsync command. Please see gsutil help rsync if you wanna to know the details.

Publish files by gsutil acl

We need to operate ACL for transferred files because files are private mode generally. To be accessible them from external tools like browser so add public-read attribute by gsutil acl.
$ gsutil -m acl -ar set public-read gs://[bucket-name]/[dir-if-exists]

Access to bucket

It's accessible to uploaded bucket from https://storage.googleapis.com/[your-bucket-name]. You can change the url to your definition domain. Please see this page.

Set metadata

The Uploaded files' content-type is automatically defined by its extension. If a file doesn't have an extension, to detect file as "text/html".
$ gsutil setmeta -h "Content-Type:text/html" \
  -h "Cache-Control:public, max-age=3600" \
  -h "Content-Disposition" "gs://bucket/*"
Please see the details; setmeta - Set metadata on already uploaded objects.

No comments:

Post a Comment