Node.JS Project to Remotely Upload Files to Google Drive Without Downloading Them Locally in Browser Using Javascript

You are currently viewing Node.JS Project to Remotely Upload Files to Google Drive Without Downloading Them Locally in Browser Using Javascript

Node.js Project to Remotely Upload Files to Google Drive Without Downloading them Locally in Browser Using Javascript

<span class="pl-k">const</span> <span class="pl-s1">split</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'split'</span><span class="pl-kos">)</span>
<span class="pl-k">const</span> <span class="pl-v">GriveUploader</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'./index'</span><span class="pl-kos">)</span>
<span class="pl-k">const</span> <span class="pl-s1">up</span> <span class="pl-c1">=</span> <span class="pl-v">GriveUploader</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
  <span class="pl-c1">client_id</span>: <span class="pl-s1">process</span><span class="pl-kos">.</span><span class="pl-c1">env</span><span class="pl-kos">.</span><span class="pl-c1">GOOGLEAUTH_CLIENT</span><span class="pl-kos">,</span>
  <span class="pl-c1">client_secret</span>: <span class="pl-s1">process</span><span class="pl-kos">.</span><span class="pl-c1">env</span><span class="pl-kos">.</span><span class="pl-c1">GOOGLEAUTH_SECRET</span><span class="pl-kos">,</span>
  <span class="pl-c1">refresh_token</span>: <span class="pl-s1">process</span><span class="pl-kos">.</span><span class="pl-c1">env</span><span class="pl-kos">.</span><span class="pl-c1">REFRESH_TOKEN</span><span class="pl-kos">,</span>
  <span class="pl-c1">drive_folder_id</span>: <span class="pl-s1">process</span><span class="pl-kos">.</span><span class="pl-c1">env</span><span class="pl-kos">.</span><span class="pl-c1">DRIVE_FOLDER_ID</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span>
 
<span class="pl-s1">process</span><span class="pl-kos">.</span><span class="pl-c1">stdin</span><span class="pl-kos">.</span><span class="pl-en">pipe</span><span class="pl-kos">(</span><span class="pl-s1">split</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">pipe</span><span class="pl-kos">(</span><span class="pl-s1">up</span><span class="pl-kos">)</span>

<span class="pl-k">const</span> <span class="pl-s1">fs</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'fs'</span><span class="pl-kos">)</span>
<span class="pl-k">const</span> <span class="pl-s1">split</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'split'</span><span class="pl-kos">)</span>
<span class="pl-k">const</span> <span class="pl-v">GriveUploader</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'./index'</span><span class="pl-kos">)</span>
 
<span class="pl-k">const</span> <span class="pl-s1">up</span> <span class="pl-c1">=</span> <span class="pl-v">GriveUploader</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
  <span class="pl-c1">client_id</span>: <span class="pl-s1">process</span><span class="pl-kos">.</span><span class="pl-c1">env</span><span class="pl-kos">.</span><span class="pl-c1">GOOGLEAUTH_CLIENT</span><span class="pl-kos">,</span>
  <span class="pl-c1">client_secret</span>: <span class="pl-s1">process</span><span class="pl-kos">.</span><span class="pl-c1">env</span><span class="pl-kos">.</span><span class="pl-c1">GOOGLEAUTH_SECRET</span><span class="pl-kos">,</span>
  <span class="pl-c1">refresh_token</span>: <span class="pl-s1">process</span><span class="pl-kos">.</span><span class="pl-c1">env</span><span class="pl-kos">.</span><span class="pl-c1">REFRESH_TOKEN</span><span class="pl-kos">,</span>
  <span class="pl-c1">drive_folder_id</span>: <span class="pl-s1">process</span><span class="pl-kos">.</span><span class="pl-c1">env</span><span class="pl-kos">.</span><span class="pl-c1">DRIVE_FOLDER_ID</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span>
 
<span class="pl-s1">fs</span><span class="pl-kos">.</span><span class="pl-en">createReadStream</span><span class="pl-kos">(</span><span class="pl-s">'input.csv'</span><span class="pl-kos">)</span> <span class="pl-c">// read the csv file</span>
  <span class="pl-kos">.</span><span class="pl-en">pipe</span><span class="pl-kos">(</span><span class="pl-s1">split</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-c">// split the content into lines</span>
  <span class="pl-kos">.</span><span class="pl-en">pipe</span><span class="pl-kos">(</span><span class="pl-s1">up</span><span class="pl-kos">)</span> <span class="pl-c">// for each lines, upload to google drive</span>

docker run -t --rm --name=up \
-v <span class="pl-s"><span class="pl-pds">$(</span>pwd<span class="pl-pds">)</span></span>/input.csv:/input.csv
-e FILENAME=X \
-e DRIVE_FOLDER_ID=X \
-e GOOGLEAUTH_CLIENT=X \
-e GOOGLEAUTH_SECRET=X \
-e REFRESH_TOKEN=X \
quocnguyen/up

const got = require('got')
const cliProgress = require('cli-progress')
const through2 = require('through2')
const url = require('url')
const bar = new cliProgress.Bar({}, cliProgress.Presets.shades_classic)
const headers = {
  'user-agent':
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'
}
 
function logThenNext(err, currentLine, next) {
  if (err) {
    console.log(`ERR : ${currentLine.toString()}`)
  }
  next()
}
 
module.exports = ({
  client_id,
  client_secret,
  refresh_token,
  drive_folder_id
}) => {
  const blobs = require('google-drive-blobs')({
    client_id,
    client_secret,
    refresh_token
  })
 
  return through2((line, enc, next) => {
    const [remote, size] = line.toString().split(',')
    console.log(`\nuploading ${remote}`)
    const filename = url
      .parse(remote)
      .pathname.split('/')
      .pop()
 
    const upload = blobs.createWriteStream(
      {
        filename,
        parent: drive_folder_id
      },
      err => logThenNext(err, line, next)
    )
 
    const download = got.stream(remote, {
      headers: headers
    })
 
    bar.start(100, 0)
 
    return download
      .on('error', err => logThenNext(err, line, next))
      .on('downloadProgress', progress => {
        bar.update(progress.percent * 100)
      })
      .pipe(upload)
  })
}

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply