Uploading Files
How to upload files using the smart concurrent upload engine.
Uploading files directly to cloud storage can be complex—you have to manage presigned URLs, compute chunk sizes, and handle network drops.
The @nyota/drive-sdk abstracts this entirely with its Smart Upload Engine.
How it Works
When you call drive.files.upload(), the SDK performs a 3-step lifecycle automatically:
- Reservation: It asks the Nyota backend if you have enough quota. The backend returns a temporary S3 presigned URL.
- Transfer: The SDK pushes the file directly to Cloudflare R2. If the file is larger than 10MB, it automatically chunks the file and uploads multiple parts concurrently.
- Commit: It notifies the backend that the transfer is complete. The backend verifies the exact bytes stored in Cloudflare and commits the record to your database.
Basic Upload
The SDK accepts a Browser File / Blob, or a Node.js Buffer / Uint8Array.
import { NyotaDrive } from "@nyota/drive-sdk";
const drive = new NyotaDrive({ apiKey: process.env.NYOTA_API_KEY });
// Example: Uploading from a browser file input
const fileInput = document.querySelector('input[type="file"]');
const file = fileInput.files[0];
const result = await drive.files.upload({
file: file,
name: "annual-report.pdf",
visibility: "private",
// Optional: folderId: "folder_abc123"
});
console.log(result.file.id); // "file_abcd123"Tracking Progress
If you are running the SDK in a browser, it utilizes native XMLHttpRequest under the hood to provide real-time, byte-accurate progress streams—even across concurrent multi-part chunks.
const result = await drive.files.upload({
file: largeVideoFile,
name: "presentation.mp4",
visibility: "public",
onProgress: (percent, bytesLoaded, totalBytes) => {
console.log(`Upload progress: ${percent}% (${bytesLoaded}/${totalBytes})`);
},
});
// Because visibility is 'public', the result contains a permanent CDN URL
console.log(result.publicUrl);
// https://assets.drive.nyotaimara.com/...Network Resilience
Mobile networks drop. The Upload Engine includes built-in exponential backoff. If Cloudflare R2 returns a 502 Bad Gateway or the connection drops during a chunk upload, the SDK will automatically retry that specific chunk up to 3 times before failing the operation.