1

Hi all I'm trying to set up Backblaze for a nextjs app, but I'm getting cors error, in the first instance I thought that it was the bucket and I set the custom cors rules but it was not because I'm getting this error on b2.authorize(). I'm using it from localhost:3000

"corsRules": [ { "allowedHeaders": [ "authorization", "content-type", "x-bz-content-sha1", "x-bz-file-name" ], "allowedOperations": [ "b2_upload_file" ], "allowedOrigins": [ "http://localhost:3000" ], "corsRuleName": "uploadFileFromLocalHostAndWebSite", "exposeHeaders": [ "x-bz-content-sha1" ], "maxAgeSeconds": 3600 } ],

Has anyone experienced this issue? Do I have to use the master key?

async function uploadToB2(file: Blob) {
 const b2 = new B2({
 applicationKeyId: process.env.NEXT_PUBLIC_APPLICATION_KEY_ID as string,
 applicationKey: process.env.NEXT_PUBLIC_APPLICATION_KEY as string,
 });
 const auth = await b2.authorize();
 console.log(auth);
}

enter image description here

enter image description here

asked Apr 29, 2024 at 16:58
3
  • I have the exact same problem, did you solve it? stackoverflow.com/q/78429497/7123519 Commented May 4, 2024 at 19:51
  • yes, in my case using the, i moved all the logic to a backend endpoint and started working fine. seems it fails from the client side if the bucket is private. Commented May 5, 2024 at 22:56
  • I just recently figured out how to make it work on the frontend with a private bucket: stackoverflow.com/a/78432306/7123519 Commented May 6, 2024 at 19:25

1 Answer 1

1

The fix to this is to set a cors rule using the cli:

b2 update-bucket --cors-rules '[
 {
 "corsRuleName": "downloadFromAnyOriginWithUpload", 
 "allowedOrigins": [
 "*" 
 ],
 "allowedHeaders": [
 "authorization",
 "content-type",
 "x-bz-file-name",
 "x-bz-content-sha1"
 ],
 "allowedOperations": [
 "b2_download_file_by_id",
 "b2_download_file_by_name",
 "b2_upload_file",
 "b2_upload_part"
 ],
 "maxAgeSeconds": 3600
 }
]' (your bucket name)

you need to authorise in the cli before you can run this command

answered May 3, 2024 at 18:54
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that but it didn't work: stackoverflow.com/q/78429497/7123519
Hi - glad you figured out the problem! Could you mark this answer as 'accepted' so that it's clear that it was answered correctly?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.