backblaze-b2 icon indicating copy to clipboard operation
backblaze-b2 copied to clipboard

CORS on b2.authorize()

Open Casal0x opened this issue 10 months ago • 3 comments

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

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); } `

image image

Casal0x avatar Apr 29 '24 13:04 Casal0x

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

iKreateCode avatar May 03 '24 18:05 iKreateCode

i was already done, thing is it wasn't working as a helper, I created a separated endpoint and it started working. something strange, that it blocks if it is been called from client side and not from server-side with next.

Casal0x avatar May 03 '24 20:05 Casal0x

Would you know how to fix cors error for downloading_file_by_name?

iKreateCode avatar May 03 '24 20:05 iKreateCode