Socket
Book a DemoInstallSign in
Socket

google-drive-uploader-js

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

google-drive-uploader-js

A Node.js module for uploading, downloading, and managing files on Google Drive using OAuth2 authentication

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
7
-89.39%
Maintainers
1
Weekly downloads
 
Created
Source

Google Drive Uploader JS

A simple and powerful Node.js module for uploading, downloading, and managing files on Google Drive using OAuth2 authentication.

Features

  • ✅ Upload files from local filesystem
  • ✅ Upload files from memory buffers
  • ✅ Download files from Google Drive
  • ✅ Create and manage folders
  • ✅ List files in folders
  • ✅ Delete files
  • ✅ Automatic MIME type detection
  • ✅ OAuth2 authentication with refresh tokens
  • ✅ Comprehensive error handling

Installation

npm install google-drive-uploader-js

Prerequisites

Before using this module, you need to:

  • Create a Google Cloud Project
  • Enable the Google Drive API
  • Create OAuth2 credentials (Client ID, Client Secret)
  • Generate a refresh token for your application

For detailed setup instructions, visit the Google Drive API documentation.

Usage

Basic Setup

const GoogleDriveUploader = require('google-drive-uploader-js');

// Initialize the uploader
const uploader = new GoogleDriveUploader(
  'your-client-id',
  'your-client-secret',
  'your-refresh-token'
);

Upload a File

async function uploadFile() {
  try {
    const result = await uploader.uploadFile(
      './local-file.pdf',     // Local file path
      'folder-id-on-drive',   // Google Drive folder ID
      'custom-filename.pdf'   // Optional: custom filename
    );
    
    console.log('File uploaded:', result.id);
  } catch (error) {
    console.error('Upload failed:', error.message);
  }
}

uploadFile();

Upload from Memory Buffer

async function uploadFromBuffer() {
  const fileData = Buffer.from('Hello World!', 'utf8');
  
  try {
    const result = await uploader.uploadFileFromData(
      fileData,
      'hello.txt',
      'folder-id-on-drive',
      'text/plain'
    );
    
    console.log('Buffer uploaded:', result.id);
  } catch (error) {
    console.error('Upload failed:', error.message);
  }
}

Create a Folder

async function createFolder() {
  try {
    const folder = await uploader.createFolder(
      'My New Folder',
      'parent-folder-id' // Optional: parent folder ID
    );
    
    console.log('Folder created:', folder.id);
  } catch (error) {
    console.error('Folder creation failed:', error.message);
  }
}

List Files in a Folder

async function listFiles() {
  try {
    const files = await uploader.getFiles('folder-id');
    
    files.forEach(file => {
      console.log(`${file.name} (${file.id})`);
    });
  } catch (error) {
    console.error('Failed to list files:', error.message);
  }
}

Download a File

async function downloadFile() {
  try {
    const fileContent = await uploader.downloadFileContent('file-id');
    
    // Save to local filesystem
    const fs = require('fs');
    fs.writeFileSync('./downloaded-file.pdf', fileContent);
    
    console.log('File downloaded successfully');
  } catch (error) {
    console.error('Download failed:', error.message);
  }
}

Delete a File

async function deleteFile() {
  try {
    await uploader.deleteFile('file-id');
    console.log('File deleted successfully');
  } catch (error) {
    console.error('Delete failed:', error.message);
  }
}

API Reference

Constructor

new GoogleDriveUploader(clientId, clientSecret, refreshToken)

Methods

  • uploadFile(filePath, folderId, customName?) - Upload file from filesystem
  • uploadFileFromData(fileData, filename, folderId, mimeType?) - Upload from buffer
  • getFiles(folderId) - List files in folder
  • getFileById(fileId, name?) - Get file information
  • deleteFile(fileId) - Delete a file
  • createFolder(name, parentFolderId?) - Create a folder
  • folderExists(name, parentFolderId?) - Check if folder exists
  • downloadFileContent(fileId) - Download file content
  • getMimeType(filePath) - Get MIME type for file

Error Handling

All methods return promises and should be wrapped in try-catch blocks or use .catch() for proper error handling.

try {
  const result = await uploader.uploadFile('./file.pdf', 'folder-id');
} catch (error) {
  if (error.message.includes('File not found')) {
    console.error('Local file does not exist');
  } else if (error.message.includes('Failed to get access token')) {
    console.error('Authentication failed - check your credentials');
  } else {
    console.error('Upload error:', error.message);
  }
}

Supported File Types

The module automatically detects MIME types for common file extensions:

  • Documents: .pdf, .doc, .docx, .txt
  • Images: .jpg, .jpeg, .png
  • Archives: .zip
  • Spreadsheets: .xls, .xlsx
  • Audio: .mp3, .wav, .aac, .m4a

For unsupported extensions, it defaults to application/octet-stream.

Testing Locally

Before publishing to npm, test your module locally:

# In your module directory
npm link

# In your test project
npm link google-drive-uploader-js

# Test the module
node test-script.js

License

MIT

Contributing

  • Fork the repository
  • Create a feature branch
  • Make your changes
  • Add tests
  • Submit a pull request

Support

If you encounter any issues, please check the Google Drive API documentation or create an issue in the repository.

Keywords

google-drive

FAQs

Package last updated on 20 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.