google-drive-sync-directory
v1.0.1
Published
A Node.js package that allows you to easily synchronize a directory with Google Drive. This package handles authentication with Google Drive using OAuth2 and monitors a specified directory for new files. When new files are detected, they are automatically
Downloads
6
Maintainers
Readme
Google Drive Sync Module
Introduction
The Google Drive Sync Module is a Node.js package designed to simplify the process of synchronizing a local directory with a Google Drive folder. It provides functionality to monitor a specified directory for new files and automatically upload them to a designated Google Drive folder. This README guide will walk you through the setup, configuration, and usage of the module.
Installation
To install the Google Drive Sync Module, ensure you have Node.js installed on your system. You can then install the package via npm:
npm install google-drive-sync-directory
Setting up Google API Credentials
Before using the module, you need to set up the necessary credentials to access Google APIs. Follow these steps to obtain and configure the required credentials:
Google API Console: Go to the Google API Console and create or select a project.
Enable APIs: Enable the Google Drive API and any other required APIs for your project.
Create OAuth Client ID: Navigate to the "Credentials" section and create an OAuth client ID. Select "Desktop app" as the application type.
Download Credentials: Download the credentials file in JSON format. This file contains the required information for authentication.
Update Environment Variables: Open the
.env
file in your project directory and update the following variables with the values from the downloaded credentials file:CLIENT_ID
: Replace with the "Client ID" value.CLIENT_SECRET
: Replace with the "Client Secret" value.REDIRECT_URL
: Replace with the redirect URL value.DRIVE_FOLDER_ID
: Replace with the ID of the Google Drive folder you want to synchronize with.- To get the
DRIVE_FOLDER_ID
for your Google Drive folder, follow these steps:
- Navigate to Google Drive.
- Locate the desired folder you want to synchronize with.
- Right-click on the folder and select "Get link" or "Share".
- The folder ID will be present in the URL after the
/folders/
or/drive/folders/
part. It will typically be a long string of characters like this1RI64MLhHqF7XL_8MZOxYLL5d0cMWtLKH
.
- To get the
WATCH_PATH
: Replace with the local folder path you want to monitor.
How to use
To use the Google Drive Sync Module in your Node.js project, import it and call the syncDirectoryWithGoogleDrive
function with the required parameters:
const syncDirectoryWithGoogleDrive = require("google-drive-sync-directory");
const clientId = "YOUR_CLIENT_ID";
const clientSecret = "YOUR_CLIENT_SECRET";
const redirectUri = "http://localhost"; // Set this to the callback URL in your OAuth settings
const folderId = "YOUR_FOLDER_ID";
const watchPath = "./path/to/monitor";
syncDirectoryWithGoogleDrive(clientId, clientSecret, redirectUri, folderId, watchPath)
.catch((error) => {
console.error("An error occurred:", error);
});
Replace YOUR_CLIENT_ID
, YOUR_CLIENT_SECRET
, YOUR_FOLDER_ID
, and WATCH_PATH
with your own values.
Authorization
If the application asks for authorization, follow these steps:
Click on the URL displayed in the terminal to grant permissions to the application.
After granting permissions, copy the authorization code from the redirected URL and paste it into the terminal.
The code will be exchanged for an access token, which will be saved to a file for future use.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Acknowledgments
The Google Drive Sync Module utilizes the googleapis
, readline-promise
, and chokidar
npm packages for Google Drive API integration, OAuth flow, and file system monitoring, respectively. We acknowledge and appreciate the contributions of the developers of these packages.