I am making a website that users can upload and download pictures (posts and profile pictures), The cloud storage I use is Backblaze b2 cloud storage, REACT for frontend, NODE & Express for backend.
This simple explanation of what I coded: React client send get request to express server, then get the B2 authorizationToken & uploadUrl back, then start uploading the picture or pictures from the frontend (look at the code below)
const url = res.dt.uploadUrl;
const authToken = res.dt.authorizationToken ;
const hash = CryptoJS.SHA1(CryptoJS.enc.Latin1.parse(myPicture));
const body = {
myPicture
};
const headers = {
Authorization: authToken,
"X-Bz-File-Name": res.dt.picName,
"Content-Length": optimisedImage.size,
"X-Bz-Content-Sha1": hash,
"X-Bz-Info-Author": "unknown",
'Access-Control-Allow-Credentials': true
};
const uploadPicRes = await axios.post(url, body, headers);
so when I run the code from a github pages website it gives me this error:
Access to XMLHttpRequest at '<backblaze uploadUrl>' from origin '<my github-pages origin>' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I have been trying to solve this issue for days now, I downloaded b2 CLI and authorize my account then tried to update the corsRules but it gives me this error:
b2 update-bucket: error: argument --corsRules: invalid validated_loads value: "'[{corsRuleName:"
I also changed my b2 bucket CORS Rules.
I am really stuck and have no way to change to another Cloud Storage right now.
You can't use the web UI to set a CORS rule to allow POSTs to upload files - you need to set a custom rule using the CLI or API. It looks like you had an error in your JSON when you tried to set one.
I followed https://www.backblaze.com/docs/cloud-storage-enable-cors-with-the-cli to create a file with a custom CORS rule, changing the rule to contain the headers you want to use. You don't need to include
Content-Length
as the browser doesn't include it in the preflight CORS request.I then used the B2 CLI to update my bucket with the rule:
I used curl to simulate the
OPTIONS
preflight request from the browser to check that it worked:Success!
To allow downloads as well as uploads, you can add another rule to
rules.json
. For example,