question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cors issue with uploading to signed url

See original GitHub issue

I have tried many configurations to get file upload with signed url. preflight request completes ok but actual upload fails with No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

This makes me thin that the cors config is ok but there is an issue with the way the url is being signed. I see a lot of people having issues with this on stack overflow but no resolutions, which makes me think that this is really a documentation issue. Below is the most lenient configuration I have tried:

cors config:

[{"maxAgeSeconds": 3600, "method": ["GET", "HEAD", "POST", "PUT"], "origin": ["*"], "responseHeader": ["Content-Type", "Access-Control-Allow-Origin"]}]

serverside code:

   app.post('/api/v1/upload/gcs-sign', function(req, res) {
     const fileName = req.body.fileName;
     const fileType = req.body.fileType;
     const file = bucket.file(fileName);

     // signed URL expires in 1 hour
     const expirationDate = new Date(
        new Date().setHours(new Date().getHours() + 1),
    );
     const config = {
        action: 'resumable',
        expires: expirationDate,
        contentType: req.body.fileType,
     };
     file.getSignedUrl(config, (err, signedRequest) => {
        if (err) {
          return res.end();
        }
       res.json({ signedRequest });
      res.end();
    })

client side code:

     uploadToGCS = (signedRequest, file) => {
      let xhr = new XMLHttpRequest();
      xhr.open('POST', signedRequest);
      xhr.onload = () => {
        this.onUploadFinish(xhr);
      };
      xhr.onerror = this.onUploadError;
     xhr.upload.onprogress = this.onUploadProgress;
     xhr.setRequestHeader('Content-Type', file.type);
     xhr.send(file);
   };

Environment details

  • OS: OSX 10.13.3
  • Node.js version: 10.7.0
  • npm version: 6.2.0
  • @google-cloud/storage version: 1.7.0

Steps to reproduce

  1. create bucket witth cors config above
  2. run server code above ( Ive left out some basic boiler plate )
  3. attempt to upload file with client side code above

Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:20 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
leonsomedcommented, Mar 15, 2021

I was using fetch with FormData to send the file, for whatever reason that did not work. I ended up doing a couple of things:

  • Adding CORS configuration to the bucket as described here https://cloud.google.com/storage/docs/configuring-cors
  • Switching from fetch to XMLHttpRequest to send the file
  • Using PUT instead of POST
  • Using Content-Type: application/octet-stream in both the server and the client

After doing all of those I finally have it working.

6reactions
mrdulincommented, Jul 29, 2019

@sebght Try change version option for getSignedUrl method from v2(default) to v4

Read more comments on GitHub >

github_iconTop Results From Across the Web

CORS. Presigned URL. S3 - Stack Overflow
I encountered this issue as well. My CORs configuration on my bucket seemed correct yet my presigned URLs were hitting CORs problems.
Read more >
How to solve CORS problems when redirecting to S3 signed ...
The implementation of signed URLs on the frontend usually uses a 2-phase fetch. First, there is a request to the backend, asking to...
Read more >
Deep dive into CORS configs on Amazon S3 | AWS Media Blog
This function returns a presigned URL which can be used in a subsequent POST to upload a file to Amazon S3.
Read more >
CORS issue with presigned URL once I add ACL: 'PublicRead'
So I'm trying to upload to Spaces using the AWS s3 presigned URL SDK, but I'm getting a CORS error whenever I set...
Read more >
How To Fix - CORS Error while uploading files on Cloudflare ...
While using R2 to upload files with presigned URLs, I started getting the following CORS issue in my browser console:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found