Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get download url #12

Closed
ghost opened this issue Feb 12, 2016 · 8 comments
Closed

Get download url #12

ghost opened this issue Feb 12, 2016 · 8 comments
Labels

Comments

@ghost
Copy link

ghost commented Feb 12, 2016

Is it possible to get file download url or do I have to download on server and handle downloadUrl my self?

@yakovkhalinsky
Copy link
Owner

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

@yakovkhalinsky
Copy link
Owner

Does that answer your question @expertsa ?

@ghost
Copy link
Author

ghost commented Feb 14, 2016

Thank you very much, it's perfectly clear !

@yakovkhalinsky
Copy link
Owner

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 notifications@github.com
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.

@yakovkhalinsky
Copy link
Owner

@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.

@ghost
Copy link
Author

ghost commented Feb 22, 2016

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

@ghost ghost closed this as completed Mar 24, 2017
@jamiesyme
Copy link
Contributor

@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);

@jamiesyme
Copy link
Contributor

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('/');

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants