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
. You can create routes for different purposes, like uploading multiple files at once. Learn more about upload routes here.
Create <Uploader />
component
We will now build our UI using pre-built components. We'll use <UploadButton />
for single file uploads.
Install it via the shadcn CLI:
We'll also use the useUploadFile
hook. The complete code looks like this:
There is also useUploadFiles
for uploading multiple files. Learn more about the hooks here.
You're done! 🎉
You can now run your app and upload images directly to any S3-compatible service!
If you plan on uploading files larger than 5GB, take a look at multipart uploads.
Learn more
Concepts
Upload Routes
Configure upload routes for different behaviors.
Client Hooks
Use client-side hooks to easily upload files.
Other Frameworks
Use Better Upload with other frameworks.
Guides
Form
Integrate Better Upload with shadcn/ui forms and React Hook Form.
TanStack Query
Use TanStack Query to manage the upload process.