@aadvik-teklabs/onedrive-uploader
v1.0.2
Published
Premium OneDrive Asset Uploader with a modern drag-and-drop UI and automated OAuth2 token management.
Maintainers
Readme
@aadvik-teklabs/onedrive-uploader
A production-ready Microsoft OneDrive asset uploader for Node.js. Handles OAuth2 refresh-token flow automatically (with in-memory token caching) and exposes a simple class-based API for uploading, listing, downloading, and deleting files via the Microsoft Graph API.
✨ Features
- 🚀 Simple API —
OneDriveStorageclass withuploadFile,listFiles,downloadFile,deleteFile - 🔐 Automatic OAuth2 — refresh-token flow with cached access tokens (auto-refreshed 60s before expiry)
- 📁 Folder support — upload to
rootor any folder by ID - 🌐 Multi-tenant — supports
commonor specific Azure AD tenant IDs - ⚡ Microsoft Graph — uses the official
@microsoft/microsoft-graph-client
📦 Installation
npm install @aadvik-teklabs/onedrive-uploader🔧 Setup
1. Register an Azure AD application
- Go to https://portal.azure.com → Azure Active Directory → App registrations → New registration.
- Add a Web redirect URI (e.g.
http://localhost:3000/callback). - Under API permissions, add delegated permissions:
Files.ReadWrite.Allandoffline_access. - Under Certificates & secrets, create a Client secret.
- Complete the OAuth2 auth-code flow once to obtain a Refresh Token (a helper script
verify-oauth.jsis included in the repo).
2. Create a .env file
MS_CLIENT_ID=your-azure-app-client-id
MS_CLIENT_SECRET=your-azure-app-client-secret
MS_TENANT_ID=common
MS_REFRESH_TOKEN=your-long-lived-refresh-token🚀 Usage
Basic upload
const { OneDriveStorage } = require('@aadvik-teklabs/onedrive-uploader');
const fs = require('fs');
const storage = new OneDriveStorage();
async function main() {
const buffer = fs.readFileSync('./photo.jpg');
const result = await storage.uploadFile(buffer, 'photo.jpg');
console.log('Uploaded:', result);
}
main().catch(console.error);Upload to a specific folder
const result = await storage.uploadFile(buffer, 'invoice.pdf', {
folderId: '01ABCD1234EFGH5678',
});List files
const files = await storage.listFiles('root'); // or a folder ID
console.log(files);Download a file
const stream = await storage.downloadFile('01ABCD1234EFGH5678');Delete a file
await storage.deleteFile('01ABCD1234EFGH5678');Express integration example
const express = require('express');
const multer = require('multer');
const { OneDriveStorage } = require('@aadvik-teklabs/onedrive-uploader');
const app = express();
const upload = multer({ storage: multer.memoryStorage() });
const storage = new OneDriveStorage();
app.post('/upload', upload.single('file'), async (req, res) => {
try {
const result = await storage.uploadFile(req.file.buffer, req.file.originalname);
res.json({ success: true, data: result });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
app.listen(3000);📖 API Reference
new OneDriveStorage(config?)
Creates a new client instance. Reads credentials from environment variables (MS_CLIENT_ID, MS_CLIENT_SECRET, MS_TENANT_ID, MS_REFRESH_TOKEN).
uploadFile(buffer, fileName, options?)
buffer—Bufferof file contentsfileName— target file nameoptions.folderId— optional folder ID (defaults to'root')
listFiles(folderId)
Returns an array of files in the given folder.
downloadFile(fileId)
Returns a download stream.
deleteFile(fileId)
Deletes a file by ID.
isAvailable()
Returns true after successful authentication.
🔒 Security
- Never commit
.envto source control. - Rotate the
MS_REFRESH_TOKENif it may have been exposed. - Use least-privilege scopes on the Azure AD app.
📝 License
MIT © Aadvik Labs
