Skip to main content

Hello everyone,
I'm making my very first Box SDK NodeJS app which should download a public non-passwrod-protected file given the public share URL. No login should be requested.
I have created the app inside the developer section and generated the proper tokens.

On the github documentation I've found this method that should be used to download a file.
https://rawgit.com/box/box-node-sdk/master/docs/jsdoc/Files.html#getReadStream
The first parameter is the `fileId` but I don't find written anywhere what this id is and where to find it if what I have is just a share link.

I've tried this code

 

var fs = require('fs'), 
BoxSDK = require('box-node-sdk'),
sdk = new BoxSDK({
clientID: '...',
clientSecret: '...'
}),
client = sdk.getAnonymousClient(),
file_id = process.argvg2].replace(/\S*\/(\S+)$/,"$1");

client.files.getReadStream(file_id, null, function(error, stream)
{
if (error) {
throw error;
}
// write the file to disk
var output = fs.createWriteStream('./output/'+file_id+".zip"); //I know for sure there will only be zip files
stream.pipe(output);
});

 

But running it with this command

nodejs index.js https://adrime.box.com/s/s5w7lzm4xfifmxrod9r7eeniv9nhtenk

I get this error:

Error: Unexpected API Response s404 Not Found] (not_found: "Not Found") 
at Object.module.exports.buildResponseError (/home/andrea/dev/node/box_api/node_modules/box-node-sdk/lib/util/errors.js:57:23)
at Object.module.exports.buildUnexpectedResponseError (/home/andrea/dev/node/box_api/node_modules/box-node-sdk/lib/util/errors.js:94:15)
at /home/andrea/dev/node/box_api/node_modules/box-node-sdk/lib/managers/files.js:148:20
at BoxClient._handleResponse (/home/andrea/dev/node/box_api/node_modules/box-node-sdk/lib/box-client.js:291:2)
at handleResponseCallback (/home/andrea/dev/node/box_api/node_modules/box-node-sdk/lib/box-client.js:233:9)
at /home/andrea/dev/node/box_api/node_modules/box-node-sdk/lib/api-request.js:285:3
at nextTickCallbackWith0Args (node.js:436:9)
at process._tickCallback (node.js:365:13)

Of course I get that the last part of the shared link is not the id.
Can you please help me in programmatically downloading a public shared file?

Thank you in advance!


For the moment I've used this solution:

var fs         = require("fs"),
request = require("request");

function downloadFile(source, target, callback)
{
var wr = fs.createWriteStream(target);

wr.on("error", function (err) {
callback(err);
});

wr.on("close", function (ex) {
callback();
});

request
.get(source)
.on("error",function(err){
callback(err);
})
.on('response', function(response)
{
if((""+response.statusCode).charAt(0) != "2")
callback({
msg: "File not found: "+response.request.href,
path: response.request.href,
toString: function(){
return this.msg;
}
});
})
.on("end", function (ex) {
console.log("request ended");
})
.pipe(wr)
}

function onDownloadComplete(err)
{
if(err)
throw err

console.log("DOWNLOAD COMPLETE");
}

function init()
{
var static_url = process.argv[2].replace(/\/s\//,"/shared/static/") + ".zip";

downloadFile(static_url, "./output/file.zip", onDownloadComplete);
}

init();

Hope this will help others.



Trying to do what you're doing.  This link shows how to get the file id.

https://github.com/box/box-node-sdk/blob/master/docs/shared-items.md

 

once i get the file id, I still get "not found" when trying to get the file tho.

 

HTH



Just got it to work, but not with the anonymous client.  switched that line to using a developer key and it worked.  Something with anonymousclient.

 



  //var client = sdk.getAnonymousClient();


  var client = sdk.getBasicClient('');



Reply