npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-google-drive

v0.0.7

Published

Manage google drive easily. Support create folders, upload files, download files, searching, etc..

Downloads

1,261

Readme

Google Drive API Library

NPM version Test Test coverage

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 {TsGoogleDrive} from "ts-google-drive";
import {google} from "googleapis";
const tsGoogleDrive = new TsGoogleDrive({keyFilename: "serviceAccount.json"});

async function auth() {
  const drive1 = new TsGoogleDrive({keyFilename: "serviceAccount.json"});
  const drive2 = new TsGoogleDrive({credentials: {client_email: "", private_key: ""}});

  // https://www.npmjs.com/package/google-auth-library
  const drive3 = new TsGoogleDrive({oAuthCredentials: {access_token: ""}});
  const drive4 = new TsGoogleDrive({oAuthCredentials: {refresh_token: ""}, oauthClientOptions: {clientId: "", clientSecret: ""}});
}

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 uploadAndDownload() {
  const folderId = "";
  const filename = "./icon.png";
  const newFile = await tsGoogleDrive.upload(filename, {parent: folderId});
  const downloadBuffer = await newFile.download();

  // you have to use "googleapis" package
  // of if you want stream
  const drive = google.drive({version: "v3", auth: newFile.client});
  const file = await drive.files.get({
    fileId: newFile.id,
    alt: 'media'
  }, {responseType: "stream"});

  file.data.on("data", data => {
    // stream data
  });
  file.data.on("end", () => {
    // stream end
  });

  // or use pipe
  const writeStream = fs.createWriteStream('./output.png');
  file.data.pipe(writeStream);
}

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 [email protected].
    • Go to your Google Drive Folder and shared the edit permission to the email address.
  • Create using serviceAccount.json
const drive1 = new TsGoogleDrive({keyFilename: "serviceAccount.json"});
  • Create using client_email and private_key (which is available inside the services account JSON)
const drive2 = new TsGoogleDrive({credentials: {client_email: "", private_key: ""}});

Using OAuth

  • You can take reference on below link how to grab an oauth access token
  • https://medium.com/@terence410/using-google-oauth-to-access-its-api-nodejs-b2678ade776f
const drive3 = new TsGoogleDrive({oAuthCredentials: {access_token: ""}});
const drive4 = new TsGoogleDrive({oAuthCredentials: {refresh_token: ""}, oauthClientOptions: {clientId: "", clientSecret: ""}});

Links

  • https://www.npmjs.com/package/googleapis
  • https://www.npmjs.com/package/google-auth-library
  • https://developers.google.com/drive