How To Deal With Google Gcloud Cross-Site Cors Problem

If you see the Cross-site CORS header blocked for your Google cloud bucket, there is a simple way to solve that by simply setting up the COR headers using the gsutil​ Google Python command line tool for dealing with Google Cloud Storage from the command line.

Set/Enabling CORS for Google Gcloud Storage Bucket(GSC):

Create a file in your project root folder, I usually call it cors-json-file.json:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
[
    {
        "origin": [
            "https://example.com",
            "https://www.example.com",
        ],
        "method": [
            "GET",
            "PUT",
            "POST",
            "DELETE"
        ],
        "responseHeader": [
            "Content-Type"
        ],
        "maxAgeSeconds": 3600
    }
]

With that, all you have to do now is run the command line tool with the following command matching the cors file:

1
gsutil cors set cors-json-file.json gs://some-files-static

some-files-static should be your Gcloud bucket name.

Get CORS for Google Gcloud Storage Bucket(GSC):

With the get command, you can get the current CORS associated with a given bucket.

1
gsutil cors get gs://some-files-static

Remove CORS for Google Gcloud Storage Bucket(GSC):

To remove or delete all cors from a given bucket, we have to replace all content of our CORS file cors-json-file.json with this empty JSON []. So our CORS file becomes:

1
2
# Json File cors-json-file.json content 
[]

And final, do the set command again to remove the cors.

1
gsutil cors set cors-json-file.json gs://some-files-static

Reference: Google. (n.d.). Cors - get or set a CORS JSON document for one or more buckets  |  cloud storage  |  google cloud. Google. Retrieved March 23, 2023, from https://cloud.google.com/storage/docs/gsutil/commands/cors#synopsis

0 Comments

12345

    00