GithubHelp home page GithubHelp logo

Get download url about backblaze-b2 HOT 8 CLOSED

yakovkhalinsky avatar yakovkhalinsky commented on June 20, 2024 1
Get download url

from backblaze-b2.

Comments (8)

yakovkhalinsky avatar yakovkhalinsky commented on June 20, 2024

I'm assuming you are wanting the file download URL.

You could do this a few of ways. One way is to use the file's ID or combine both the bucket name and the filename.

First thing you need to do would be to get the base download URL from the auth command.
b2.auth()

From the auth response you get the downloadUrl:

{
    accountId: '...',
    apiUrl: '...',
    downloadUrl: 'https://z000.backblaze.com'
}

You should probably cache this somewhere so you don't need to run b2.auth() just to get it.

Then if you know the bucket name and you know the filename you just pull them together like so:

var baseDownloadUrl = authRespose.downloadUrl;
var bucketName = 'foo';
var fileName = 'beep-boop.png';
var fullDownloadUrl = [ 
    baseDownloadUrl,
    bucketName,
    fileName
].join('/');

To give you https://z000.backblaze.com/foo/beep-boop.png

Or if you have the file's ID, which you could get from uploading the file using b2.uploadFile(...).

E.g. from the uploadResponse get the fileId like so:

{
    ...
    fileId: 'abcd234987324babdsakjhdasdsaa-dhkfhjkad`
    ...
}

Pull together a string like this:
Then if you know the bucket name and you know the filename you just pull them together like so:

var baseDownloadUrl = authRespose.downloadUrl;
var fileId = uploadResponse.fileId;
var fullDownloadUrl = [ 
    baseDownloadUrl,
    'b2api',
    'v1',
    'b2_download_file_by_id?' + fileId
].join('/');

Should give you something like:
https://z000.backblaze.com/b2api/v2/b2_download_file_by_id?abcd234987324babdsakjhdasdsaa-dhkfhjkad

from backblaze-b2.

yakovkhalinsky avatar yakovkhalinsky commented on June 20, 2024

Does that answer your question @expertsa ?

from backblaze-b2.

expertsa avatar expertsa commented on June 20, 2024

Thank you very much, it's perfectly clear !

from backblaze-b2.

yakovkhalinsky avatar yakovkhalinsky commented on June 20, 2024

I'll have a look at how the download URL's work later today and I'll post my
findings here :)

On Feb 16 2016, at 1:26 am, Expert SA [email protected]
wrote:

Sorry to bother you again, do you know why those url are showing me the file
instead of downloading it?
I mean I know that right click save as can make it but if I a user wants to
download 6 files, it's easier if he just have to click on download rather than
right clicking on each files and save as.

I hope it's possible, or maybe this will be possible after Beta.

from backblaze-b2.

yakovkhalinsky avatar yakovkhalinsky commented on June 20, 2024

@expertsa as far as I can tell, unless you send the mime type as an octet-stream, the browser will try and open it.

Here is a great article about the HTML5 download attribute:
https://davidwalsh.name/download-attribute

That might help you with your problem.

from backblaze-b2.

expertsa avatar expertsa commented on June 20, 2024

Hi, thanks, working like a charm with the download attribute.

from backblaze-b2.

jamiesyme avatar jamiesyme commented on June 20, 2024

@yakovkhalinsky Are you open to the idea of exposing a function for this? Specifically: https://github.com/yakovkhalinsky/backblaze-b2/blob/master/lib/actions/file.js#L261

The example you gave above isn't very complicated, but it'd be far more convenient to write something like:

await b2.authorize();
const url = await b2.getFileDownloadUrl(fileId);

from backblaze-b2.

jamiesyme avatar jamiesyme commented on June 20, 2024

For future reference, the example code for downloading a file by fileId in the above comment has a bug in the query string.

The corrected code looks like:

var baseDownloadUrl = authResponse.downloadUrl;
var fileId = uploadResponse.fileId;
var fullDownloadUrl = [ 
    baseDownloadUrl,
    'b2api',
    'v1',
    'b2_download_file_by_id?fileId=' + fileId
].join('/');

from backblaze-b2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.