Andrea Silvestri
Andrea Silvestri

Reputation: 1134

box-node-sdk downloading a shared public file

I'm making a NodeJS app which should download a file given the public share URL that doesn't request login auth. I have created the app inside the developer section and generated the proper tokens.

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

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.argv[2].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 [404 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)

Can anyone help me in programmatically downloading a public shared file from box.com?

Thank you in advance!

Upvotes: 0

Views: 866

Answers (1)

Andrea Silvestri
Andrea Silvestri

Reputation: 1134

At the moment I've found this solution.

To me it works pretty well. I hope it will be useful to others as well.

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";
    console.log(static_url);

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

init();

Upvotes: 1

Related Questions