Skip to content

Instantly share code, notes, and snippets.

@thisjt
Created June 29, 2023 21:05
Show Gist options
  • Save thisjt/bc4bf8122156555203a09002924f9e41 to your computer and use it in GitHub Desktop.
Save thisjt/bc4bf8122156555203a09002924f9e41 to your computer and use it in GitHub Desktop.
Function to Upload a (Zip) File to Google Drive
const keys = require('./auth.json')
const fs = require('fs')
const {JWT} = require('google-auth-library')
const {drive} = require('googleapis/build/src/apis/drive')
// DELETE ALL FILES in "apis" folder except "drive" to slim down "googleapis" file size
function uploadFileToGdrive() {
const auth = new JWT({
email: keys.client_email,
key: keys.private_key,
scopes: ['https://www.googleapis.com/auth/drive'],
})
const service = drive({version: 'v3', auth})
const fileMetadata = {
name: 'testdb.zip',
mimeType: 'application/zip',
parents: ['FolderID'], // Share FolderID to Service Account
}
const media = {
mimeType: 'application/zip',
body: fs.createReadStream('./fileToUpload.zip'),
}
try {
const file = service.files.create({
resource: fileMetadata,
media: media,
fields: 'id',
})
} catch (err) {
throw err
}
}
uploadFileToGdrive()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment