Download a file from Google Drive (stream to disk), upload to different folder
@dylburger
code:
data:privatelast updated:4 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 1,000,000+ developers using the Pipedream platform
steps.
trigger
HTTP API
Deploy to generate unique URL
This workflow runs on Pipedream's servers and is triggered by HTTP / Webhook requests.
steps.
googledrive_stream_file_to_tmp
Streams a file from Google Drive to a file in the /tmp directory of your workflow
auth
(auths.google_drive)
params
File ID

The ID of the file in Google Drive you'd like to download

 
string ·params.fileId
File Path

You can save the file anywhere to the /tmp directory in your workflow, e.g. /tmp/file.pdf.

 
/tmp/file.pdf
string ·params.filePath
code
async (params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
}
38
const fs = require("fs")
const { google } = require('googleapis')

const driveAuth = new google.auth.OAuth2()
driveAuth.setCredentials({ access_token: auths.google_drive.oauth_access_token })
const drive = await google.drive({ version: 'v3', auth: driveAuth });

const { fileId, filePath } = params
return drive.files
  .get({ fileId, alt: 'media' }, { responseType: 'stream' })
  .then(res => {
    return new Promise((resolve, reject) => {
      console.log(`writing to ${filePath}`);
      const dest = fs.createWriteStream(filePath);
      let progress = 0;

      res.data
        .on('end', () => {
          console.log('Done downloading file.');
          resolve(filePath);
        })
        .on('error', err => {
          console.error('Error downloading file.');
          reject(err);
        })
        .on('data', d => {
          progress += d.length;
          if (process.stdout.isTTY) {
            process.stdout.clearLine();
            process.stdout.cursorTo(0);
            process.stdout.write(`Downloaded ${progress} bytes`);
          }
        })
        .pipe(dest);
    });
  });
steps.
upload_file_to_folder_by_file_path
Allows you to upload a file previously-saved to the /tmp directory, by passing in the path to that file
auth
(auths.google_drive)
params
Filename
Test.jpeg
string ·params.filename
Parent Folders

A list of parent folder IDs, entered manually or retrieved from a previous file list operation

[0]:
 
array ·params.parents
Filetype
 
string ·params.filetype
File path

The path to the file, e.g. /tmp/myFile.csv .

 
string ·params.pathToFile
code
async (params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
}
22
const fs = require("fs")
const {google} = require('googleapis')

const driveAuth = new google.auth.OAuth2()
driveAuth.setCredentials({ access_token: auths.google_drive.oauth_access_token })
const drive = await google.drive({ version: 'v3', auth: driveAuth });

this.uploadResponse = (await drive.files.create({
  resource: {
    name: params.filename,
    originalFilename: params.filename,
    parents: params.parents,
  },
  media: {
    mimeType: params.filetype,
    uploadType: "media",
    body: fs.createReadStream(params.pathToFile)
  },
  fields: '*'
})).data