Informatics

Information about various topics

Ads Here

8.8.17

Google cloud - Enable CORS using GSUTIL

There are 2 ways to enable CORS,
1. Using "gsutil"
2. Using Google Cloud Terminal
Google cloud - Enable CORS

1. Using "gsutil"

The easiest way to configure your data for CORS is with the gsutil command line tool. 

Install "gsutil"

  1. Download the Cloud SDK installer. The installer is signed by Google Inc.
  2. Launch the installer and follow the prompts. If Python 2.7.9 or later is not installed on your system, make sure the option to install Bundled Python is checked.
  3. After installation has completed, accept the following options:
    • Start Cloud SDK Shell
    • Run gcloud init
  4. The installer starts a terminal window and runs the gcloud init command.
  5. The default installation does not include the App Engine extensions required to deploy an application using gcloud commands. These components can be installed using the Cloud SDK component manager.
The installation instructions for gsutil are available at https://cloud.google.com/storage/docs/gsutil_install.

Authenticate "gsutil" and Enable CORS

Once you've installed gsutil and authenticated with it, you can use it to configure CORS.
For example, if you just want to allow object downloads from your custom domain, put this data in a file named cors.json (replacing "https://example.com" with your domain):
[
  {
    "origin": ["https://example.com"],
    "method": ["GET"],
    "maxAgeSeconds": 3600
  }
]
Then, run this command (replacing "exampleproject.appspot.com" with the name of your bucket):
gsutil cors set cors.json gs://exampleproject.appspot.com
and you should be set.
If you need a more complicated CORS configuration, check out the docs at https://cloud.google.com/storage/docs/cross-origin#Configuring-CORS-on-a-Bucket.

2.Using Google Cloud Terminal

Google cloud - Enable CORS
Login to your google cloud console: https://console.cloud.google.com/homeClick on “Activate Google Cloud Shell” in the upper right corner (see picture below):
At the bottom of your window, a shell terminal will be shown, where gcloud and gsutil are already available. Execute the command below. It creates a json-file which is needed to setup the cors-settings for your bucket:
1
echo '[{"origin": ["*"],"responseHeader": ["Content-Type"],"method": ["GET", "HEAD"],"maxAgeSeconds": 3600}]' > cors-config.json
This will allow every domain to access your bucket via CORS-Requests.  If you want to restrict the access one or more specific domains, add their URL to the array, e.g.:
1
echo '[{"origin": ["https://yourdomain.com"],"responseHeader": ["Content-Type"],"method": ["GET", "HEAD"],"maxAgeSeconds": 3600}]' > cors-config.json
Replace with your actual bucketname in the following command to update the cors-settings from your bucket
1
gsutil cors set cors-config.json gs://
To check if everything worked as expected, you can get the cors-settings of a bucket with the following command:
1
gsutil cors get gs://

No comments:

Post a Comment