Configuration. See SetupFileUploadOptions.
Object with:
PUT — Next.js route handler for file uploads (auth required, multipart form).uploadFile(keys, { url?, blob? }) — Server-side: upload a file from URL or Blob to S3.deleteFile(url) — Server-side: delete a file by its S3 URL.getUploadURL(keys) — Server-side: get a presigned PUT URL (5-minute expiry).getDownloadURL(keys) — Get the public download URL for a key.Builds the public download URL for one or more keys in the configured S3 bucket.
// app/api/(rest)/file/route.ts
import { setupFileUpload } from "naystack/file";
export const { PUT } = setupFileUpload({
onUpload: async ({ url, type, userId, data }) => {
if (type === "DealDocument" && url) {
const payload = data as { dealId: number; fileName: string };
const [row] = await db.insert(DocumentsTable).values({
dealId: payload.dealId, fileURL: url, fileName: payload.fileName,
}).returning();
return row ?? {};
}
return {};
},
// Optional: customize the S3 key (defaults to UUID)
getKey: async ({ type, userId }) => `${type}/${userId}/${crypto.randomUUID()}`,
});
Configures file upload for S3. Returns a
PUTroute handler (for the client to upload files) and server-side helpers.Mount the
PUThandler on your file upload API route (e.g.app/api/(rest)/file/route.ts). The client can then useuseFileUpload()fromnaystack/file/clientto upload files with the auth token.AWS credentials are read from env vars:
AWS_ACCESS_KEY_ID,AWS_ACCESS_KEY_SECRET,AWS_REGION,AWS_BUCKET.