cors issue with uploading to signed url
See original GitHub issueI 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
- create bucket witth cors config above
- run server code above ( Ive left out some basic boiler plate )
- attempt to upload file with client side code above
Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:20 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
After doing all of those I finally have it working.
@sebght Try change
version
option forgetSignedUrl
method fromv2
(default) tov4