Get unlimited public & private packages + package-based permissions with npm Pro.Get started »

ts-google-drive

0.0.3 • Public • Published

Google Drive API Library

NPM version Test Test coverage David deps

Manage google drive easily. Support create folders, upload files, download files, searching, etc.. The library is build with Google Drive API v3.

Features

  • Create Folders
  • Upload Files
  • Download Files (as Buffer)
  • Powerful file query tools
  • Empty Trash

Usage

import {TsGooleDrive} from "ts-google-drive";
 
const tsGoogleDrive = new TsGooleDrive({keyFilename: "serviceAccount.json"});
 
async function getSingleFile() {
    const fileId = "";
    const file = await tsGoogleDrive.getFile(fileId);
    if (file) {
        const isFolder = file.isFolder;
    } 
}
 
async function listFolders() {
    const folderId = "";
    const folders = await tsGoogleDrive
        .query()
        .setFolderOnly()
        .inFolder(folderId)
        .run();
}
 
async function createFolder() {
    const folderId = "";
    const newFolder = await tsGoogleDrive.createFolder({
        name: "testing",
        parent: folderId,
    });
 
    // try to search for it again
    const foundFolder = await tsGoogleDrive
        .query()
        .setFolderOnly()
        .setModifiedTime("=", newFolder.modifiedAt)
        .runOnce();
}
 
async function uploadFile() {
    const folderId = "";
    const filename = "./icon.png";
    const newFile = await tsGoogleDrive.upload(filename, {parent: folderId});
    const downloadBuffer = await newFile.download();
}
 
async function search() {
    const folderId = "";
    const query = await tsGoogleDrive
        .query()
        .setFolderOnly()
        .inFolder(folderId)
        .setPageSize(3)
        .setOrderBy("name")
        .setNameContains("New");
    
    // or you can use any query https://developers.google.com/drive/api/v3/search-files
    query.setQuery("name = 'hello'");
 
    while (query.hasNextPage()) {
        const folders = await query.run();
        for (const folder of folders) {
            await folder.delete();
        }
    }
}
 
async function emptyTrash() {
    await tsGoogleDrive.emptyTrash();
}

Using Service Account

  • Create a Google Cloud Project
  • Create Service Account
    • Service account details > Choose any service account name > CREATE
    • Grant this service account access to project > CONTINUE
    • Grant users access to this service account ( > CREATE KEY
    • Save the key file into your project
  • Enable Drive API
  • To access shared folder
    • Open the JSON key file, you will find an email xxx@xxx.iam.gserviceaccount.com.
    • Go to your Google Drive Folder and shared the edit permission to the email address.
  • Create using serviceAccount.json
const tsGoogleDrive = new TsGooleDrive({keyFilename: "serviceAccount.json"});
  • Create using client_email and private_key
const credentials = {client_email: "", private_key: ""}; // these can be found inside the json file
const tsGoogleDrive = new TsGooleDrive({credentials});

Using OAuth

  • How to generate an oauth token is not covered here
  • But you can create one easily via https://developers.google.com/oauthplayground/
  • Select Drive API v3 with scopes "https://www.googleapis.com/auth/drive"
  • Authorize and get the Access token
  • Create using accessToken
const tsGoogleDrive = new TsGooleDrive({accessToken: ""});

Links

Install

npm i ts-google-drive

DownloadsWeekly Downloads

2

Version

0.0.3

License

MIT License

Unpacked Size

43.6 kB

Total Files

15

Last publish

Collaborators

  • avatar