30 Days of node
Day 29 : Uploading files to dropbox using node.js

How to upload file to dropbox using it's API and node.js







30 days of node - Nodejs tutorial series

Overview

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 .

Contents:

  1. Getting the access token from dropbox by creating an app.
  2. uploading file on dropbox using node.js
  3. Celebrating the success with screenshots from working app.

Prerequisites

  1. Node.js installed
  2. Dropbox account

Directory Structure


Getting the access token

Follow the given link and login to your dropbox account : Dropbox Developers.

  • Let's start creating the application.



  • Choose the details :
  • Select you Dropbox API , type of access allowed and also give a suitable name to your application.



  • CLick on the create app button.



  • Congratulations you have successfully created the app.Now click on the generate Token button in order to create your access token.



  • Congratulations you have successfully created access token.



uploading file to dropbox using node.js

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

Run : We can run it in the following way :
													
>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": "#################
 ##############"}										
													
												

Screenshots

  • Screenshot 1 : Background application up and running

  • Screenshot 2 : Uploaded content on Dropbox

Summary

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 .



Get the code on