Search code examples
google-cloud-platformcorsgoogle-cloud-storagegoogle-api-nodejs-clientpre-signed-url

The CORS header is present on PUT requests but not on OPTIONS requests


I have a Google Cloud Storage bucket with the following CORS configuration:

[
    {
      "origin": ["http://localhost:8080"],
      "responseHeader": [
        "Content-Type",
        "Access-Control-Allow-Origin",
        "Origin"],
      "method": ["GET", "HEAD", "DELETE", "POST", "PUT", "OPTIONS"],
      "maxAgeSeconds": 3600
    }
]

I am generating a signed URL with the following code:

let bucket = storage.bucket(bucketName);
let file = bucket.file(key);

const options = {
    version: "v4",
    action: "write",
    expires: Date.now() + 15 * 60 * 1000, // 15 minutes
    contentType: "application/zip"
};

let url = await file.getSignedUrl(options))[0];

For my requests I am using the following headers:

Origin: http://localhost:8080
Content-Type: application/zip

When I try using a PUT request to upload the data everything works fine and I get the Access-Control-Allow-Origin header containing my Origin. But when I do a OPTIONS request with the exact same headers it fails to return the Access-Control-Allow-Origin header. I have tried many alterations to my CORS config but none have worked like:


Solution

  • I solved my own problem with some help from my colleagues, when I was testing the function in Postman the CORS header was not sent as a response to the OPTIONS request because the request was missing the Access-Control-Request-Method header. When I added this header it worked fine.