Switch to uploadthing
How to change the default storage provider to uploadthing.
uploadthing is a platform for storing files in the cloud. It’s a great alternative to AWS S3 and it’s free for small projects. Here’s how to switch the default storage provider to uploadthing.
1. Swap out the required dependencies
First, uninstall the existing dependencies from the Storage package…
… and install the new dependencies…
2. Update the environment variables
Next, update the environment variables across the project, for example:
Additionally, replace all instances of BLOB_READ_WRITE_TOKEN
with UPLOADTHING_TOKEN
in the packages/env/index.ts
file.
3. Update the existing storage files
Update the index.ts
and client.ts
to use the new uploadthing
packages:
4. Create new files for the storage client
We’ll also need to create a couple of new files for the storage package to handle the Tailwind CSS classes and SSR.
5. Create a file router in your app
Create a new file in your app’s lib
directory to define the file router. This file will be used to define the file routes for your app, using your Auth package to get the current user.
6. Create a route handler
Create a new route handler in your app’s api
directory to handle the file routes.
7. Update your root layout
Update your root layout to include the StorageSSRPlugin
. This will add SSR hydration and avoid a loading state on your upload button.
8. Update your app’s Tailwind config
Update your app’s Tailwind config to include the withStorage
function. This will add the necessary classes to your upload button.
9. Create your upload button
Create a new component for your upload button. This will use the generateUploadButton
function to create a button that will upload files to the imageUploader
endpoint.
Now you can import this component into your app and use it as a regular component.
10. Advanced configuration
uploadthing is a powerful platform that offers a lot of advanced configuration options. You can learn more about them in the uploadthing documentation.
File Routes
Learn how to define file routes
Security
How to protect different parts of the UploadThing flow.