Quickstart
You can setup file upload in your Next.js app in a few minutes with Better Upload. If you are using another framework or a backend server, check out Other Frameworks.
This guide will walk you through the steps to set it up with Next.js, but the steps are similar for other frameworks.
Prerequisites
Before you start, make sure you have the following:
- A Next.js project.
- An S3-compatible bucket. You can use AWS S3, Cloudflare R2, or any other.
Uploading your first image
Set up the Route Handler
The Route Handler will create pre-signed URLs, which the client uses to upload files directly to the S3 bucket. The recommended location for Next.js is app/api/upload/route.ts
.
Change your-bucket-name
to your S3 bucket name, and configure the S3 client as needed.
In the example above, we create the upload route demo
for single images. You can create multiple routes for different purposes, like uploading more than one file. Learn more about routes here.
Place the Upload Button
The upload button component is used to upload single files. Put it in a component/page.
You can install it via the shadcn CLI:
Whether you put it in a component or a page, mark it with 'use client';
, as
the pre-built components need to be used in client components.
You're done! 🎉
You can now run your app and upload an image directly to any S3-compatible service!
If you plan on uploading files larger than 5GB, take a look at multipart uploads.
Learn more
Guides
Upload Routes
Configure upload routes for different behaviors.
Client Hooks
Use client-side hooks to easily upload files.
Other Frameworks
Use Better Upload in other frameworks.