API
and node.js
We've reached 29th day of our node.js tutorial series and here we are with
our third project tutorial in which we will learn how to upload files to dropbox using dropbox's api
and
node.js
.
Follow the given link and login to your dropbox account : Dropbox Developers.
Now let's move onto the coding part.Although the code is self explanatory but informative comments are given where ever needed :
//Name of the file : dropbox-file-upload.js
//Including the required moduless
var request = require('request');
var fs = require('fs');
//enter your access token
var access_token = "WRITE_YOUR_ACCESS_TOKEN_HERE";
//Name of the file to be uploaded
var filename = '44.png';
//reading the contents
var content = fs.readFileSync(filename);
//write your folder name in place of YOUR_PATH_TO_FOLDER
// For example if the folder name is njera then we can write it in the following way :
// "Dropbox-API-Arg": "{\"path\": \"/njera/"+filename+"\",\"mode\": \"overwrite\",\"autorename\": true,\"mute\": false}"
options = {
method: "POST",
url: 'https://content.dropboxapi.com/2/files/upload',
headers: {
"Content-Type": "application/octet-stream",
"Authorization": "Bearer " + access_token,
"Dropbox-API-Arg": "{\"path\": \"/YOUR_PATH_TO_FOLDER/"+filename+"\",\"mode\": \"overwrite\",\"autorename\": true,\"mute\": false}",
},
body:content
};
request(options,function(err, res,body){
console.log("Err : " + err);
console.log("res : " + res);
console.log("body : " + body);
})
>node dropbox-file-upload.js
Err : null
res : [object Object]
body : {"name": "44.png", "path_lower": "/njera/44.png", "path_display": "/njera/44.p
ng", "id": "id:##################", "client_modified": "2017-12-19T22:10:22Z", "server_modified":
"2017-12-19T22:10:22Z", "rev": "1372903ae0", "size": 599, "content_hash": "#################
##############"}
In this chapter of 30 days of node tutorial series, we learned about how we can create
developers account on dropbox.We also learned about how we can upload files
to dropbox using it's API and node.js
.