snippets_js
Integration
New integration from credential
const googleDriveClient = await yepcode.integration.googleDrive("credential-slug");
New integration from plain authentication data
const { google } = require("googleapis");
const googleDriveCredentials = {
"type": "service_account",
"project_id": "PROJECT_ID",
"private_key_id": "KEY_ID",
"private_key": "-----BEGIN PRIVATE KEY-----\nPRIVATE_KEY\n-----END PRIVATE KEY-----\n",
"client_email": "SERVICE_ACCOUNT_EMAIL",
"client_id": "CLIENT_ID",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/SERVICE_ACCOUNT_EMAIL",
"universe_domain": "googleapis.com",
};
const auth = new google.auth.GoogleAuth({
credentials: googleDriveCredentials,
scopes: ['https://www.googleapis.com/auth/drive']
});
const googleDriveClient = google.drive({ version: 'v3', auth });
Find a Folder
Find a folder in 'My Drive' by name
const { data: folder } = await googleDriveClient.files.list({
q: `name='folderName' and mimeType='application/vnd.google-apps.folder'`,
});
console.log(folder.files[0]);
Find a folder in 'Shared Drives' by name
const { data: folder } = await googleDriveClient.files.list({
q: `name='folderName' and mimeType='application/vnd.google-apps.folder'`,
corpora: "drive",
driveId: sharedDrivesRootFolderId, // The sharedDrivesRootFolderId can be obtained in the root folder URL "https://drive.google.com/drive/u/2/folders/---sharedDrivesRootFolderId---"
includeItemsFromAllDrives: true,
supportsAllDrives: true,
});
console.log(folder.files[0]);
Create a Subfolder
Create a subfolder
const { data: subfolder } = await googleDriveClient.files.create({
resource: {
name: "folderName",
mimeType: "application/vnd.google-apps.folder",
parents: ["folderId1", "folderId2"], // You can obtain folderId finding the folder first (seen in the previous examples) and getting the property id (folder.files[0].id)
}
});
console.log(subfolder);
Upload a File to a Folder
Upload a file to a folder
//First we need to create a Readable stream from a Base 64 file content
const { Readable } = require('stream');
const readableStreamFile = Readable.from(Buffer.from(base64FileContent, 'base64'));
const { data: file } = await googleDriveClient.files.create({
media: {
mimeType: "application/octet-stream",
body: readableStreamFile,
},
resource: {
name: "fileName",
parents: ["folderId1", "folderId2"], // You can obtain folderId finding the folder first (seen in the previous examples) and getting the property id (folder.files[0].id)
},
});
console.log(file);