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.

Tuesday, October 11, 2016

Getting HTTP status code by cURL

I often use cURL command at the command line on Linux and OS X to fetch information by HTTP requests. Sometimes I just want to know only the HTTP status code. The option -v, which means verbose printing, prints too much information though I don't need.

-w "%{http_code}"

A more specific way to print out just the HTTP status code is something along the lines of:
$ curl -sI -o /dev/null -w "%{http_code}" http://www.example.org/
The option -w means to print assigned format below:
-w, --write-out 
        Make curl display information on stdout after a completed transfer. The format is a string that may
        contain plain text mixed with any number of variables. The format can be  specified  as  a  literal
        "string",  or  you  can  have curl read the format from a file with "@filename" and to tell curl to
        read the format from stdin you write "@-".

        The variables present in the output format will be substituted by  the  value  or  text  that  curl
        thinks  fit,  as  described  below. All variables are specified as %{variable_name} and to output a
        normal % you just write them as %%. You can output a newline by using \n, a carriage return with \r
        and a tab space with \t.

        NOTE: The %-symbol is a special symbol in the win32-environment, where all occurrences of % must be
        doubled when using this option.
Too simple and many format types are available. show man page to just type man curl. The option -I might be added to improve response load performance. This parameter just request for status headers of response, without download response body.