What is @googleapis/drive?
@googleapis/drive is an npm package that provides a client library for accessing Google Drive API. It allows developers to interact with Google Drive to perform various operations such as file management, permissions handling, and more.
What are @googleapis/drive's main functionalities?
List Files
This feature allows you to list files in the user's Google Drive. The code sample demonstrates how to authenticate and list the first 10 files, displaying their IDs and names.
const { google } = require('@googleapis/drive');
const auth = new google.auth.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/drive.readonly'],
});
async function listFiles() {
const authClient = await auth.getClient();
const drive = google.drive({ version: 'v3', auth: authClient });
const res = await drive.files.list({
pageSize: 10,
fields: 'files(id, name)',
});
console.log('Files:', res.data.files);
}
listFiles().catch(console.error);
Upload File
This feature allows you to upload a file to Google Drive. The code sample demonstrates how to authenticate, create file metadata, and upload a file from the local filesystem.
const { google } = require('@googleapis/drive');
const fs = require('fs');
const auth = new google.auth.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/drive.file'],
});
async function uploadFile() {
const authClient = await auth.getClient();
const drive = google.drive({ version: 'v3', auth: authClient });
const fileMetadata = {
name: 'photo.jpg',
};
const media = {
mimeType: 'image/jpeg',
body: fs.createReadStream('files/photo.jpg'),
};
const res = await drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id',
});
console.log('File Id:', res.data.id);
}
uploadFile().catch(console.error);
Create Folder
This feature allows you to create a new folder in Google Drive. The code sample demonstrates how to authenticate and create a folder with specified metadata.
const { google } = require('@googleapis/drive');
const auth = new google.auth.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/drive.file'],
});
async function createFolder() {
const authClient = await auth.getClient();
const drive = google.drive({ version: 'v3', auth: authClient });
const fileMetadata = {
name: 'New Folder',
mimeType: 'application/vnd.google-apps.folder',
};
const res = await drive.files.create({
resource: fileMetadata,
fields: 'id',
});
console.log('Folder Id:', res.data.id);
}
createFolder().catch(console.error);
Delete File
This feature allows you to delete a file from Google Drive. The code sample demonstrates how to authenticate and delete a file by its ID.
const { google } = require('@googleapis/drive');
const auth = new google.auth.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/drive.file'],
});
async function deleteFile(fileId) {
const authClient = await auth.getClient();
const drive = google.drive({ version: 'v3', auth: authClient });
await drive.files.delete({
fileId: fileId,
});
console.log('File deleted');
}
deleteFile('your-file-id').catch(console.error);
Other packages similar to @googleapis/drive
googleapis
The 'googleapis' package is a comprehensive client library for accessing various Google APIs, including Google Drive. It provides similar functionalities to @googleapis/drive but also includes support for other Google services like Gmail, Calendar, and more.
google-drive
The 'google-drive' package is another library for accessing Google Drive API. It provides functionalities for file management and sharing. It is similar to @googleapis/drive but may have different API design and usage patterns.
drive
Manages files in Drive including uploading, downloading, searching, detecting changes, and updating sharing permissions.
Installation
$ npm install @googleapis/drive
Usage
All documentation and usage information can be found on GitHub.
Information on classes can be found in Googleapis Documentation.
License
This library is licensed under Apache 2.0. Full license text is available in LICENSE.
Contributing
We love contributions! Before submitting a Pull Request, it's always good to start with a new issue first. To learn more, see CONTRIBUTING.
Questions/problems?
Crafted with ❤️ by the Google Node.js team