Skip to content

Instantly share code, notes, and snippets.

@HR
Created November 24, 2020 18:41
Show Gist options
  • Save HR/a27c056e7b8a6dd2085e8029d00560b9 to your computer and use it in GitHub Desktop.
Save HR/a27c056e7b8a6dd2085e8029d00560b9 to your computer and use it in GitHub Desktop.
Google Drive v3 API NodeJs get folder by name if exists or create a new one otherwise
let folderId
const parentFolderId = ''
const folderName = ''
// Check if folder already exists
const folderSearch = await drive.files.list({
q: `mimeType = 'application/vnd.google-apps.folder' and name = '${folderName}' and '${parentFolderId}' in parents`,
fields: 'nextPageToken, files(id, name)',
spaces: 'drive'
})
if (folderSearch.data.files.length) {
// Folder already exists
folderId = folderSearch.data.files[0].id
console.log('Already exists', folderId)
} else {
// Folder does not exist so create
const newFolder = await drive.files.create({
resource: {
name: folderName,
mimeType: 'application/vnd.google-apps.folder',
parents: [parentFolderId]
},
fields: 'id'
})
folderId = newFolder.data.id
console.log('Not exist, created', folderId)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment