yakovkhalinsky / backblaze-b2

Node.js Library for the Backblaze B2 Storage Service
MIT License
191 stars 59 forks source link

How do I get the url of my uploaded image? #116

Closed mattfranciswork0 closed 2 years ago

mattfranciswork0 commented 3 years ago

Hey all,

I'm trying to upload a file to b2 and then store that uploaded file's URL in my database so I can refer to it. As the docs said, b2_get_upload_url gives an upload URL that will be used before uploading.

        {    

        "bucketId" : "4a48fe8875c6214145260818",     

        "uploadUrl" : "https://pod-000-1005-03.backblaze.com/b2api/v2/b2_upload_file?cvt=c001_v0001005_t0027&bucket=4a48fe8875c6214145260818",    

            "authorizationToken" :  
        "2_20151009170037_f504a0f39a0f4e657337e624_9754dde94359bd7b8f1445c8f4cc1a231a33f714_upld" 

        }

However, when I try to access uploadUrl, I receive:

        {"code": "method_not_allowed", 
        "message": "method not allowed: GET", 
        "status": 405 }

I also notice that my image in my b2 uses other links such as: https://f002.backblazeb2.com/file/werwerw/thomasRhett.jpg

https://werwerwer.s21.us-west-0222.backblazeb2.com/thomasRhett.jpg

https://f002.backblazeb2.com/b2api/v1/b2_download_file_by_id?fileId=werwerwerwerwerm0050

So how can I actually access my new image's URL?

Thanks! Any help would be appreciated :)

poacher2k commented 2 years ago

The URL returned is not to be used before uploading, but for uploading. Use the uploadUrl and the authorizationToken to upload your image with a POST request.

See https://www.backblaze.com/b2/docs/b2_upload_file.html for more details

metadaddy commented 2 years ago

You can construct the download URL according to the mechanism documented at b2_download_file_by_name:

The base URL to use comes from the b2_authorize_account call, and looks something like https://f345.backblazeb2.com. The "f" in the URL stands for "file", and the number is the cluster number containing your account. To this base, you add "file/", your bucket name, a "/", and then the name of the file. The file name may itself include more "/" characters.

If you have a bucket named "photos", and a file called "cute/kitten.jpg", then the URL for downloading that file would be: https://f345.backblazeb2.com/file/photos/cute/kitten.jpg.

In terms of backblaze-b2, this is simply:

const auth = await b2.authorize();
const downloadURL = auth.data.downloadUrl + '/file/' + bucketName + '/' + fileName;
varundeva commented 2 years ago

Same issue i have.. i can able to upload file, i have to store download url somewhere in database

metadaddy commented 2 years ago

@varundeva Read my comment immediately above yours. You don't need anything from the B2 response to create the download URL.

varundeva commented 2 years ago

Yes. i got.. im using native url format which is showing in b2 bucket website. download file using fileId. which is fine to store in DB.