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).

upload.js
// 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.

Terminal
npm install multer
server.js
const 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);
});
index.html
<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

server.js
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();
NameTypeWhat it is
idstringThe file's ID: use it to get, send or delete the file.
namestring or nullThe name it was uploaded with.
contentTypestringIts type, like image/png.
sizenumberIts size in bytes.
createdAtstringWhen 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.

server.js
app.get("/files/:id", async (req, res) => {
  await cloud.files.send(res, req.params.id);
});

Save a file on your server, or stream it

server.js
await cloud.files.download(fileId, "./copies/1027.pdf");

const stream = await cloud.files.stream(fileId); // a Node.js Readable

Delete a file

server.js
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

NameTypeWhat it is
NOT_FOUND404No file with that ID in your workspace.
PAYLOAD_TOO_LARGE413The file is over the platform's upload limit.
STORAGE_QUOTA_EXCEEDED413Your workspace's storage is full: delete files first.
SECRETS_SEALED503The vault is sealed, so files can't be encrypted or decrypted yet.
FILE_NOT_FOUND0upload() got a path with no file at it.
NextUser sign in →