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

gdrive-node-uploader

v1.2.4

Published

A Node.js library to upload, download, and manage files on Google Drive using a service account.

Downloads

181

Readme

🚀 GDrive Node Service Uploader

Programmatically upload, download, and manage files on Google Drive using a Service Account and Node.js.

✨ Features

  • ⬆️ Upload Files: Seamlessly upload files to Google Drive.
  • ⬇️ Download Files: Download files from Google Drive to your local system.
  • 🗑️ Delete Files: Remove files from Google Drive.
  • 📊 Progress Tracking: Track the progress of upload and download operations.

📦 Installation

npm install gdrive-node-service-uploader

🚀 Usage

Prerequisites

Ensure you have a Google Service Account and the credentials JSON file. Set the path to your credentials file in the environment variable SERVICE_ACCOUNT_KEY_FILE.

  1. Create a Project on Google Cloud Platform:

    • Go to the Google Cloud Console.
    • Click on the project dropdown and select New Project.
    • Enter the project name and click Create.
  2. Enable the Google Drive API:

    • In the API Library, search for "Google Drive API".
    • Click on Google Drive API and then click Enable.
  3. Create a Service Account:

    • Navigate to the Service Accounts page.
    • Click Create Service Account.
    • Enter the service account name and description, then click Create.
    • In the next step, assign the role Project > Editor and click Continue.
    • Skip granting additional users access and click Done.
  4. Create a Service Account Key:

    • On the Service Accounts page, find your newly created service account.
    • Click on the service account and go to the Keys tab.
    • Click Add Key > Create New Key.
    • Select JSON and click Create.
    • A JSON file with your credentials will be downloaded.
  5. Set Up Environment Variable:

    • Store the downloaded JSON file securely.
    • Set the SERVICE_ACCOUNT_KEY_FILE environment variable to the path of the JSON file.
    export SERVICE_ACCOUNT_KEY_FILE=/path/to/your/service-account-file.json

Setup

  1. ⬆️ Upload a File
import { uploadFileToGoogleDrive } from 'gdrive-node-service-uploader';

const filePath = 'path/to/your/file.zip';

uploadFileToGoogleDrive({
  filePath,
  onProgress: (progress) => {
    console.log(`Upload Progress: ${progress}%`);
  }
}).then((result) => {
  console.log('Upload result:', result);
});
  1. ⬇️ Download a File
import { downloadFileFromGoogleDrive } from 'gdrive-node-service-uploader';

const fileId = 'your-file-id';
const downloadPath = 'path/to/download/location.zip';

downloadFileFromGoogleDrive({
  fileId,
  downloadPath,
  onProgress: (progress) => {
    console.log(`Download Progress: ${progress}%`);
  }
}).then((result) => {
  console.log('Download result:', result);
});
  1. 🗑️ Delete a File
import { deleteFileFromGoogleDrive } from 'gdrive-node-service-uploader';

const fileId = 'your-file-id';

deleteFileFromGoogleDrive({ fileId }).then((result) => {
  console.log('Delete result:', result);
});

🌟 Example

An example script to upload, download, and delete a file:

import path from 'path';
import ProgressBar from 'progress';
import { uploadFileToGoogleDrive, downloadFileFromGoogleDrive, deleteFileFromGoogleDrive } from 'gdrive-node-service-uploader';

const uploadAndDownloadExample = async () => {
  const baseDir = path.resolve(__dirname, '..');
  const filePath = path.join(baseDir, 'example_archive.zip');
  const downloadPath = path.join(baseDir, 'example_archive_downloaded.zip');

  try {
    const uploadProgressBar = new ProgressBar('⬆️ Uploading [:bar] :percent :etas', {
      total: 100,
      width: 40,
      complete: '=',
      incomplete: ' ',
    });

    const uploadResult = await uploadFileToGoogleDrive({
      filePath,
      onProgress: (progress) => {
        uploadProgressBar.update(progress / 100);
      },
    });

    console.log('\nUpload result:', uploadResult);

    if (uploadResult.status) {
      console.log('File uploaded successfully. Initiating download...');

      const downloadProgressBar = new ProgressBar('⬇️ Downloading [:bar] :percent :etas', {
        total: 100,
        width: 40,
        complete: '=',
        incomplete: ' ',
      });

      const downloadResult = await downloadFileFromGoogleDrive({
        fileId: uploadResult.downloadUrl.split('/')[5],
        downloadPath,
        onProgress: (progress) => {
          downloadProgressBar.update(progress / 100);
        },
      });

      console.log('\nDownload result:', downloadResult);

      if (downloadResult.status) {
        console.log('File downloaded successfully. Initiating deletion...');

        const deleteResult = await deleteFileFromGoogleDrive({
          url: uploadResult.downloadUrl,
        });

        console.log('Delete result:', deleteResult);
      } else {
        console.error('Download failed. Skipping deletion.');
      }
    } else {
      console.error('Upload failed. Skipping download and deletion.');
    }
  } catch (error) {
    console.error('Error during file operations:', error);
  }
};

uploadAndDownloadExample();

🌍 Environment Variables

  • SERVICE_ACCOUNT_KEY_FILE: Path to the Google Service Account credentials JSON file.
  • DEFAULT_DOWNLOAD_PATH: Default path for downloaded files.

📜 License

MIT