Last active
March 15, 2023 16:20
-
-
Save davestevens/6f376f220cc31b4a25cd to your computer and use it in GitHub Desktop.
Download images from Google Drive folder using Node.js Google library
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
var google = require("googleapis"), | |
drive = google.drive("v2"), | |
fs = require("fs"); | |
var config = { | |
"client_id": "client_id", | |
"client_secret": "client_secret", | |
"scope": "scope", | |
"redirect_url": "redirect_rul", | |
"tokens": { | |
"access_token": "access_token", | |
"token_type": "Bearer", | |
"id_token": "id_token", | |
"refresh_token": "refresh_token", | |
"expiry_date": "expiry_date" | |
} | |
}; | |
function buildClient() { | |
var client = new google.auth.OAuth2( | |
config.client_id, | |
config.client_secret, | |
config.redirect_url | |
); | |
client.setCredentials(config.tokens); | |
return client; | |
} | |
var client = buildClient(), | |
folderId = "folderId"; // The Google Drive Folder Id | |
// Request all image files from folder | |
drive.children.list({ | |
auth: client, | |
folderId: folderId, | |
q: "mimeType contains 'image' and trashed = false" | |
}, function(error, response) { | |
if (error) { return console.log("ERROR", error); } | |
response.items.forEach(function(item) { | |
var file = fs.createWriteStream("./" + item.title); | |
file.on("finish", function() { | |
console.log("downloaded", item.title); | |
}); | |
// Download file | |
drive.files.get({ | |
auth: client, | |
fileId: item.id, | |
alt: "media" | |
}).pipe(file); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
final code for me
or