@bernierllc/user-storage-service
v0.4.0
Published
Orchestration service for routing file uploads to user-owned or application-owned storage backends
Readme
@bernierllc/user-storage-service
Orchestration service for routing file uploads to user-owned or application-owned storage backends with OAuth token management and billing tracking.
Features
- Multi-storage routing - Route files to Dropbox, Google Drive, or S3
- OAuth token management - Automatic token refresh for user-owned storage
- Billing integration - Track S3 usage for billing purposes
- Storage connection status - Check if users have connected storage providers
- Usage tracking - Monitor storage usage for billing
Installation
npm install @bernierllc/user-storage-serviceUsage
Basic Setup
import { UserStorageService } from '@bernierllc/user-storage-service';
// Token storage implementation
const tokenStorage: TokenStorage = {
async store(userId, provider, tokens) {
await db.saveTokens(userId, provider, tokens);
},
async retrieve(userId, provider) {
return await db.getTokens(userId, provider);
},
async delete(userId, provider) {
await db.deleteTokens(userId, provider);
}
};
// Billing service implementation (optional)
const billingService: BillingService = {
async recordUsage(usage) {
await db.recordStorageUsage(usage);
},
async getUsage(userId, period) {
return await db.getStorageUsage(userId, period);
}
};
// Create service instance
const storageService = new UserStorageService({
userId: currentUser.id,
tokenStorage,
billingService
});Upload Files
// Upload to user's Dropbox
const results = await storageService.uploadFiles(files, {
storageType: 'dropbox',
path: 'documents/2024',
public: false
});
// Upload to user's Google Drive
const results = await storageService.uploadFiles(files, {
storageType: 'google-drive',
path: 'uploads',
public: true
});
// Upload to application's S3 (billed)
const results = await storageService.uploadFiles(files, {
storageType: 's3',
path: 'user-uploads',
public: true
});Check Storage Connection
// Check if user has connected Dropbox
const hasDropbox = await storageService.isStorageConnected('dropbox');
// Check if user has connected Google Drive
const hasGoogleDrive = await storageService.isStorageConnected('google-drive');
// S3 is always available (application-owned)
const hasS3 = await storageService.isStorageConnected('s3'); // Always trueGet Storage Usage
// Get S3 usage for billing
const usage = await storageService.getStorageUsage('s3');
console.log(`Total bytes: ${usage.totalBytes}`);
console.log(`File count: ${usage.fileCount}`);API
UserStorageService
Constructor
constructor(config: UserStorageServiceConfig)Methods
uploadFiles(files: FileData[], options: UserStorageUploadOptions): Promise<UploadResult[]>
Upload files to selected storage backend.
getUserTokens(provider: 'dropbox' | 'google-drive'): Promise<OAuthTokens | null>
Get user's OAuth tokens for a storage provider.
isStorageConnected(provider: StorageType): Promise<boolean>
Check if user has connected a storage provider.
getStorageUsage(provider: 's3'): Promise<StorageUsage>
Get storage usage for user (S3 only, requires billing service).
Environment Variables
For OAuth token refresh:
DROPBOX_CLIENT_ID=your_dropbox_client_id
DROPBOX_CLIENT_SECRET=your_dropbox_client_secret
DROPBOX_REDIRECT_URI=https://yourapp.com/oauth/dropbox/callback
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
GOOGLE_REDIRECT_URI=https://yourapp.com/oauth/google-drive/callbackFor S3 (application-owned storage):
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
AWS_REGION=us-east-1
AWS_S3_BUCKET=your-bucket-nameLicense
Copyright (c) 2025 Bernier LLC. See LICENSE file for details.
