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.
Update the index.ts and client.ts to use the new uploadthing packages:
Copy
import { createUploadthing } from 'uploadthing/next';export { type FileRouter, createRouteHandler } from 'uploadthing/next';export { UploadThingError as UploadError, extractRouterConfig } from 'uploadthing/server';export const storage = createUploadthing();
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.
apps/app/app/lib/upload.ts
Copy
import { currentUser } from '@repo/auth/server';import { type FileRouter, UploadError, storage } from '@repo/storage';export const router: FileRouter = { imageUploader: storage({ image: { maxFileSize: '4MB', maxFileCount: 1, }, }) .middleware(async () => { const user = await currentUser(); if (!user) { throw new UploadError('Unauthorized'); } return { userId: user.id }; }) .onUploadComplete(({ metadata, file }) => ({ uploadedBy: metadata.userId }),};
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.
uploadthing is a powerful platform that offers a lot of advanced configuration options. You can learn more about them in the uploadthing documentation.