cloudrop
v1.0.0
Published
Fast, resumable, direct-to-bucket file uploads for Cloudflare R2 and S3-compatible storage.
Readme
cloudrop
Direct-to-bucket file uploads for Cloudflare R2 and S3-compatible storage. Your server decides who may upload and signs short-lived links; the browser sends the bytes straight to the bucket, with progress, retries, pause and resume after a reload.
From 1.0 the public API follows semantic versioning (Stability); every change is in the changelog.
npm i cloudrop # or: bun add cloudrop, pnpm add cloudropQuickstart
The server, in lib/cloudrop.ts. Its storage is r2(), which reads CLOUDROP_SECRET and the four CLOUDROP_R2_*
variables on the first request, so a build needs none of them; files go under the route's name, avatar/:
// lib/cloudrop.ts
import { createCloudrop, route, unauthorized } from "cloudrop";
export const cloudrop = createCloudrop({
routes: {
avatar: route({
accept: "image/*",
maxSize: "5MB",
authorize: async ({ request }) => {
const user = await getUser(request);
return user ? { userId: user.id } : unauthorized();
},
}),
},
});Mount it. In Next.js, app/api/cloudrop/[...all]/route.ts:
import { toNextHandler } from "cloudrop/next";
import { cloudrop } from "../../../../lib/cloudrop";
export const { GET, POST, OPTIONS } = toNextHandler(cloudrop);Upload from React. createUploadHooks needs only the server's type (a type import), so no server code reaches the
browser. Create the hooks once, in their own module when several components upload:
"use client";
import { createUploadHooks } from "cloudrop/react";
import type { cloudrop } from "../lib/cloudrop";
const { useUpload } = createUploadHooks<typeof cloudrop>();
export function AvatarPicker() {
const { files, getInputProps } = useUpload("avatar", { accept: "image/*" });
const avatar = files.at(-1);
return (
<label>
Avatar <input {...getInputProps()} />
{avatar && `${avatar.status} ${Math.round(avatar.progress * 100)}%`}
{avatar?.error && <p role="alert">{avatar.error.message}</p>}
</label>
);
}Without React, createCloudropClient from cloudrop/client gives you upload(), which resolves to { data, error }.
When users sign in, give each user a client of their own, and at sign-out await close({ abort: true }) before
forget(): Signed-in users. Pass
the signed-in user's ID as account, and a route with a store refuses an upload that another tab's sign-in moved to
someone else. A bearer token for your API goes in the client's headers option, which never reaches the bucket.
onComplete runs at least once per file: a client that lost the answer to /complete asks again, so make it
idempotent before it sends an email or calls a webhook. To keep a row per upload in your own database, with a quota per
user, revocation, a listing of each user's files, one file per user on routes such as avatars (replace: true),
erasure of a user's files (api.eraseOwner) and a sweep for abandoned uploads, pass a store from cloudrop/adapters/sql,
cloudrop/adapters/drizzle or cloudrop/adapters/prisma. With a store, onComplete runs once per file, except after a
crash, a throw or a run longer than its lease; only its writes through tx on Postgres with transaction: true commit
exactly once: Database store.
Testing your app
cloudrop/testing runs your server and the real client in one process, in Vitest, Bun or node:test, with no bucket,
browser or network:
import { createCloudrop } from "cloudrop";
import { testBucket, testClient } from "cloudrop/testing";
const bucket = testBucket();
const cloudrop = createCloudrop({ storage: bucket.storage, secret, routes: { avatar } });
const client = testClient(cloudrop, { bucket, headers: { cookie: "session=alice" } });
const { data, error } = await client.upload("avatar", new File([png], "me.png", { type: "image/png" }));
expect(bucket.object(data!.file.key)?.size).toBe(png.byteLength);memoryStore() stands in for a database store. Examples for authorize, metadata, quotas, onComplete and multipart
files: Testing your app.
Checking what was uploaded
The browser declares each file's type, and accept checks that declaration. contentChecks() from cloudrop/plugins
refuses web pages, programs, scripts and unnamed XML types at /upload, and checks the bytes at /complete: it reads
the first 4 KiB of the stored file and refuses, and deletes, a file whose content contradicts its type or is one of
those, whatever it was sent as. Scanning for malware is a recipe of about ten lines, in the same guide.
import { contentChecks } from "cloudrop/plugins";
export const cloudrop = createCloudrop({ storage, routes, plugins: [contentChecks()] });What it recognises, what it refuses and its limits: Plugins.
Bucket setup
The browser uploads to the bucket directly, so the bucket needs a CORS rule that allows PUT from your page's origin
with the headers cloudrop signs (content-type, content-disposition, if-none-match, x-amz-meta-cloudrop-id) and
exposes ETag and Retry-After, plus a lifecycle rule that aborts incomplete multipart uploads. Without the CORS
rule, uploads fail, usually with BUCKET_BLOCKED. npx cloudrop setup --origin http://localhost:3000 prints both rules
and the commands that set them, --apply adds both and keeps the bucket's other rules, and npx cloudrop doctor
--origin <origin> checks the settings, the bucket and its rules. Both rules, with the Wrangler and AWS CLI commands:
Bucket setup, and the command:
The cloudrop command.
Documentation
All docs · Server · Client · React · Database store · Deployment · Resume after reload · Testing your app · Plugins · Hono guide · Errors · Troubleshooting · Stability · Security · The cloudrop command
The same guides ship inside the package, for this exact version: node_modules/cloudrop/docs/README.md lists them, so
your editor and your coding agent can read them offline.
Runs on Node.js 26+, Bun, Deno and Cloudflare Workers, with zero runtime dependencies. cloudrop/react needs React 18
or 19, an optional peer dependency.
License
MIT. Not affiliated with Cloudflare.
