Created
June 29, 2023 21:05
-
-
Save thisjt/bc4bf8122156555203a09002924f9e41 to your computer and use it in GitHub Desktop.
Function to Upload a (Zip) File to Google Drive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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