-1

I have a backend service which has download URL, let's say example.com/download/{filkey}

Whenever this url is called it will create google bucket CDN signed URL. And response of example.com/download/{filkey} API will have 308 (Permanent Redirect) Status code and location = {Google signed CDN URL} in its response. which means whenever I will hit example.com/download/{filkey} in browser it will be redirected to the CDN URL and image will be shown in browser and its working as expected.

But, issue occurs when I try to call example.com/download/{filkey} in JS code. It shows me No 'Access-Control-Allow-Origin' header is present

  fetch('http://example.com/download/abc_file', {
      method: 'GET',
      mode : 'cors', 
      headers: {
         'Authorization' : 'Bearer <Token>' // Token  to validate user at backend   
      },
    })
    .then(res => {
        console.log('Work Done');
        console.log(res);
        console.log('');
        console.log(res,res.body,res.headers.get('location'),'content.res')})
    .catch(err => {
        console.log('Error');
        console.log(err);
        console.log('');
        console.log(err,'content.res.err');});

enter image description here

I have also updated CORS policy for my google bucket. But still no luck This is my current CORS policy for google bucket

[{"maxAgeSeconds": 3600, "method": ["GET", "OPTIONS", "HEAD", "PATCH", "PUT", "POST"], "origin": ["*"], "responseHeader": ["*"]}]

enter image description here

I am stuck this issue from long days. I have tried lots of different ways by adding/removing headers in my request/response. It works when I add mode : no-cors in my JS request. But, I can't do that as the backend service needs the user token.

Thanks in Advance

Jayendra Singh
  • 147
  • 2
  • 10

1 Answers1

0

You need to include an Access-Control-Allow-Origin header in the preflight response from your backend service, not just the response from Google Cloud Storage.

elving
  • 1,441
  • 8
  • 7