Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Storage Signed URL gives CORS error

Greetings again,

I am giving my client a signed url to upload files to cloud storage.  However I am getting the following error when calling the URL : 

Cross-Origin Resource Sharing error: PreflightMissingAllowOriginHeader

I am aware that I need to setup the server to receive the call using the header : 

Access-Control-Allow-Origin

However I don't know where to add this header to storage since the call is being made directly to storage and not to my server...

 

Thanx for the help !

Solved Solved
0 3 9,709
1 ACCEPTED SOLUTION

Hello @icepaco33!

Check out this documentation: Set up and view CORS configurations.

To setup the CORS configuration on a bucket, you can use the gcloud CLI. Use the following steps to set a CORS configuration on your bucket:

  1. Create a JSON file with the CORS configuration you would like to apply. Check the configuration examples for sample JSON files.
  2. Use the gcloud storage buckets update command with the --cors-file flag:
    gcloud storage buckets update gs://BUCKET_NAME --cors-file=CORS_CONFIG_FILE

To view the CORS configuration for a bucket, you can use the gcloud CLI and input the command:

gcloud storage buckets describe gs://BUCKET_NAME --format="default(cors_config)"

 If the above options don't work, you can contact Google Cloud Support to further look into your case. Let me know if it helped, thanks!

View solution in original post

3 REPLIES 3

Hello @icepaco33!

Check out this documentation: Set up and view CORS configurations.

To setup the CORS configuration on a bucket, you can use the gcloud CLI. Use the following steps to set a CORS configuration on your bucket:

  1. Create a JSON file with the CORS configuration you would like to apply. Check the configuration examples for sample JSON files.
  2. Use the gcloud storage buckets update command with the --cors-file flag:
    gcloud storage buckets update gs://BUCKET_NAME --cors-file=CORS_CONFIG_FILE

To view the CORS configuration for a bucket, you can use the gcloud CLI and input the command:

gcloud storage buckets describe gs://BUCKET_NAME --format="default(cors_config)"

 If the above options don't work, you can contact Google Cloud Support to further look into your case. Let me know if it helped, thanks!

The  "configuration examples" is incorrect. I got error "ERROR: 'str' object has no attribute 'items'" when running the command.

The correct format is 
```
[ { "origin": ["*"], "responseHeader": ["Content-Type"], "method": ["GET", "PUT"], "maxAgeSeconds": 3600 } ]
```
I found this from the gcloud storage bucket update command documentation here
https://cloud.google.com/sdk/gcloud/reference/storage/buckets/update

I had the same problem and the format from pengxu is indeed correct. Here it is formatted with the example:

[
{
"origin": ["https://your-example-website.appspot.com"],
"method": ["GET"],
"responseHeader": ["Content-Type"],
"maxAgeSeconds": 3600
}
]