Utilities for interacting with Google Drive API
npm install mkt-google-drive-utilsbash
npm install mkt-google-drive-utils
`
🎯 Quick Start
1️⃣ Import the module and initialize it
`js
import drive from "mkt-google-drive-utils";
// Initialize with your CLIENT_ID
drive.init("YOUR_GOOGLE_CLIENT_ID");
`
2️⃣ Authenticate the user to get the token
Call drive.authenticate() when the user clicks a button.
Important: This should be called in a user interactive mode. That is why is better to call in a button click event.
`js
await drive.authenticate();
console.log("User authenticated successfully!");
`
📁 File Operations
$3
Retrieve a list of files inside a folder.
`js
const files = await drive.readFolder("FOLDER_ID");
console.log(files);
`
Each file in the array contains:
`json
[
{ "id": "FILE_ID", "name": "example.json" },
{ "id": "FILE_ID", "name": "notes.txt" }
]
`
$3
Save a JavaScript object as a JSON file inside a Google Drive folder.
`js
const myObject = { theme: "dark", lang: "en" };
await drive.createJsonFile("FOLDER_ID", "settings.json", myObject);
`
$3
Read a JSON file and parse its content.
`js
const settings = await drive.readJsonFile("FOLDER_ID", "settings.json");
console.log(settings); // Output: { theme: "dark", lang: "en" }
You can also read a file by file ID:
const settings = await drive.readJsonFile("FILE_ID");
`
$3
Save plain text content inside a Google Drive folder.
`js
await drive.createFile("FOLDER_ID", "notes.txt", "Hello, Google Drive!");
`
$3
Retrieve text content from a file.
`js
const content = await drive.readFile("FOLDER_ID", "notes.txt");
console.log(content); // Output: "Hello, Google Drive!"
`
Alternatively, read by file ID:
`js
const content = await drive.readFile("FILE_ID");
`
$3
Move a file from one folder to another.
`js
await drive.moveFile("SOURCE_FOLDER_ID", "notes.txt", "DESTINATION_FOLDER_ID");
`
$3
Delete.
`js
await drive.deleteFile("SOURCE_FOLDER_ID", "notes.txt");
await drive.deleteFileId("FILE_ID");
`
🔧 Example HTML Page
You can also check the HTML example from the [example] folder in github.
📝 index.html
`html
MK Google Drive Utils Example
Google Drive Helper Example
`
📝 app.js
`js
import { CLIENT_ID, FOLDER_ID } from "./config.js";
import drive from "../src/drive.js";
// Initialize
drive.init(CLIENT_ID);
// Authenticate
document.getElementById("authButton").addEventListener("click", async () => {
await drive.authenticate();
log("Authentication successful!");
});
// Now perform any action
document.getElementById("createFile").addEventListener("click", async () => {
await drive.createJsonFile(FOLDER_ID, "example.json", { message: "Hello, world!" });
log("JSON file created!");
});
document.getElementById("readFile").addEventListener("click", async () => {
const content = await drive.readJsonFile(FOLDER_ID, "example.json");
log("Read JSON: " + JSON.stringify(content, null, 2));
});
document.getElementById("listFiles").addEventListener("click", async () => {
const files = await drive.readFolder(FOLDER_ID);
log("Files in folder: " + JSON.stringify(files, null, 2));
});
function log(message) {
document.getElementById("output").textContent = message;
}
`
📝 config.js
`js
export const CLIENT_ID = "YOUR_GOOGLE_CLIENT_ID";
export const FOLDER_ID = "YOUR_FOLDER_ID";
``