SDK for Node.js
Files
Store files encrypted, list them, send them to your users and delete them. Big files stream: they are never held in memory.
Upload a file
upload() takes a path on disk, a Buffer, a stream, or a file from a form. It returns the file's details: keep its id (in your database, for example).
// A file on disk: the name and type come from the path.
const file = await cloud.files.upload("./invoices/1027.pdf");
console.log(file.id, file.name, file.size);
// Text or bytes you made in your code.
await cloud.files.upload(Buffer.from("Hello"), { name: "hello.txt" });Upload from a form
HTML forms send files as multipart/form-data. Read them with multer, then pass req.file to the SDK.
npm install multerconst multer = require("multer");
const upload = multer({ limits: { fileSize: 20 * 1024 * 1024 } }); // 20 MB
app.post("/photos", upload.single("photo"), async (req, res) => {
if (!req.file) return res.status(400).json({ message: "Choose a photo." });
const file = await cloud.files.upload(req.file);
res.status(201).json(file);
});<form action="/photos" method="post" enctype="multipart/form-data">
<input type="file" name="photo" accept="image/*" required>
<button type="submit">Upload</button>
</form>List and look up files
const files = await cloud.files.list(); // every file, newest first
const one = await cloud.files.get(fileId); // one file's details
const { usedBytes, limitBytes } = await cloud.files.usage();| Name | Type | What it is |
|---|---|---|
id | string | The file's ID: use it to get, send or delete the file. |
name | string or null | The name it was uploaded with. |
contentType | string | Its type, like image/png. |
size | number | Its size in bytes. |
createdAt | string | When it was uploaded (ISO 8601). |
Send a file to the browser
send(res, id) streams the file to your user as a download, with its original name.
app.get("/files/:id", async (req, res) => {
await cloud.files.send(res, req.params.id);
});Save a file on your server, or stream it
await cloud.files.download(fileId, "./copies/1027.pdf");
const stream = await cloud.files.stream(fileId); // a Node.js ReadableDelete a file
await cloud.files.delete(fileId);Its key is destroyed first, so it can never be read again, even from an old copy.
Errors you may see
| Name | Type | What it is |
|---|---|---|
NOT_FOUND | 404 | No file with that ID in your workspace. |
PAYLOAD_TOO_LARGE | 413 | The file is over the platform's upload limit. |
STORAGE_QUOTA_EXCEEDED | 413 | Your workspace's storage is full: delete files first. |
SECRETS_SEALED | 503 | The vault is sealed, so files can't be encrypted or decrypted yet. |
FILE_NOT_FOUND | 0 | upload() got a path with no file at it. |