Upload straight from the browser to Cloudinary, unsigned with a preset, or signed by your server. Large files upload in resumable chunks.
import { cloudinaryAdapter } from "@uploadcn/core"Files go directly from the browser to Cloudinary's upload API, with real progress. You don't need the Cloudinary SDK.
Tested with Cloudinary
Tested end to end against a Cloudinary account: unsigned and signed uploads, browser
CORS, chunked uploads resumed after a dropped connection, expired and tampered
signatures, and deletes. Large files need @uploadcn/core 0.1.4 or later.
Unsigned uploads
In the Cloudinary console, open Settings → Upload presets → Add upload preset and set Signing mode to Unsigned. Then restrict the preset there: allowed formats, max file size, folder, moderation. Your cloud name is on the console dashboard. Neither the cloud name nor the preset name is a secret.
const adapter = cloudinaryAdapter({
cloudName: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME!,
uploadPreset: "user-uploads",
folder: "avatars",
})The config generator has the .env template and a
command that creates the unsigned preset through Cloudinary's Admin API.
Anyone can use an unsigned preset
The preset name is public. Use unsigned presets only for low-risk uploads, and lock the preset down. For anything tied to a user, sign uploads on your server.
Signed uploads
Your server decides what's allowed and signs the parameters with your API secret. The secret never reaches the browser.
Add a signing route
import { signCloudinaryParams } from "@uploadcn/server"
export async function POST(request: Request) {
const session = await auth()
if (!session) return new Response("Unauthorized", { status: 401 })
const params = (await request.json()) as Record<string, string>
// Only sign what this user may upload.
if (params.folder !== `users/${session.user.id}`) {
return new Response("Forbidden", { status: 403 })
}
return Response.json(
await signCloudinaryParams(params, {
apiKey: process.env.CLOUDINARY_API_KEY!,
apiSecret: process.env.CLOUDINARY_API_SECRET!,
})
)
}Ask it for a signature
const adapter = cloudinaryAdapter({
cloudName: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME!,
folder: `users/${userId}`,
sign: (params) =>
fetch("/api/cloudinary", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(params),
}).then((response) => response.json()),
})Large files
Files larger than chunkSize (default 20 MB) upload in chunks using Cloudinary's
chunked upload API. Cloudinary needs every chunk except the last to be at least 5 MiB
(5,242,880 bytes), so a smaller chunkSize is raised to that. Each finished chunk is
saved as resume state, so a retry (or a reload with
persistence) continues from the last chunk instead of
starting over.
Cloudinary accepts a signature for one hour, so with signed uploads sign may be called
again: when an upload resumes, or between chunks of an upload that runs that long. Your
signing route already handles it; it just signs a new timestamp.
Your plan's maximum file size applies to the whole file, chunked or not.
The result
Each successful item's result is Cloudinary's upload response:
onSuccess={(item) => {
item.result.secure_url // https://res.cloudinary.com/…
item.result.public_id
item.result.width
}}Errors
When Cloudinary refuses an upload, the error carries Cloudinary's own reason:
onError={(item, error) => {
error.message // "Cloudinary: Upload preset not found"
error.status // 400
error.details // Cloudinary's full response body
}}Common ones:
Upload preset not found: the preset name is wrong, or it belongs to another cloud.Invalid Signature(401): your signing route used the wrong API secret, or the browser sent parameters other than the ones it signed. The message shows the string Cloudinary expected you to sign.Stale request: the signature is older than one hour. The adapter re-signs on its own when it resumes.resources with extension bin are not allowed: Cloudinary refuses some file extensions. Limit the accepted types in your upload component.
Options
Prop
Type