22

Following Google docs, when using GoogleCloud Storage console:

mysef@myproject:~$ cat cors-json-file.json 
[ 
  {
    "origin": ["*"],
    "method": ["GET"],
    "maxAgeSeconds": 3600
  } 
]

then I get the following error:

myself@myproject:~$ gsutil cors set cors-json-file.json gs://mybucket
Setting CORS on gs://mybucket/...
ArgumentException: JSON CORS data could not be loaded from: [ 
  {
    "origin": ["*"],
    "method": ["GET"],
    "maxAgeSeconds": 3600
  } 
]

same error when I remove "method", "maxAgeSeconds" or add "responseHeader".

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
comte
  • 3,092
  • 5
  • 25
  • 41

3 Answers3

21

after so many tries, I ended up with modifing json file to:

$ cat cors-json-file.json 
[{
    "origin": ["*"],
    "method": ["GET"],
    "maxAgeSeconds": 3600
}]

and... it worked! Note that example from Google documentation is my first case (with [{ on 2 lines)

comte
  • 3,092
  • 5
  • 25
  • 41
5

I had the same problem and it was caused by the type of quotes used. By opening the json-file in a plain text editor and change the quote to standard ones fixed the problem.

lagren
  • 51
  • 1
  • 1
  • What do you call "standard ones" ? JSON quotes are double, by definition. – comte Mar 21 '20 at 14:23
  • 2
    @comte, by "standard" I mean the regular U+0022 (QUOTATION MARK). Some text editors or CMS might replace the standard quotation mark with U+201C and U+201D (LEFT DOUBLE QUOTATION MARK and RIGHT DOUBLE QUOTATION MARK). If the file content has been copied from somewhere, this might be hard to spot. – lagren Jun 30 '20 at 07:51
  • I see what you mean now. But that was not the case for me (and Tyler seems). It wasn't quotation marks the problem. – comte Jul 01 '20 at 08:07
  • Thanks. This solution works for me. Previously I tried to edit the JSON in the online editor, it probably corrupted from there. After I replace all the double quotes inside vscode, it works fine. – Ras Nov 29 '22 at 04:20
1

I got the same error when I tried to set the CORS settings with the trailing comma "," as shown below:

[
    {
      "origin": ["http://localhost:8000"],
      "method": ["GET"],
      "responseHeader": ["Content-Type"],
      "maxAgeSeconds": 3600, 
    }                   // ↑ Trailing comma
]                          

So, I removed the trailing comma "," as shown below then I could set the CORS settings successfully without no error:

[
    {
      "origin": ["http://localhost:8000"],
      "method": ["GET"],
      "responseHeader": ["Content-Type"],
      "maxAgeSeconds": 3600 
    }                   // ↑ No trailing comma
]                          
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129