Skip to content

Architecture · By Rivtor · Updated

Upload files from Next.js with presigned S3 URLs

Generate a short-lived, narrowly scoped upload URL on the authenticated server, then send the file directly from the browser to object storage. Keep credentials server-side and verify the completed object before treating the upload as an application record.

See the Rivtor deployment workflow →

Authorize before signing

A presigned URL grants a temporary capability; it is not inherently tied to the current browser session. Authenticate the request, verify the user’s upload allowance and choose the bucket and object key on the server. Do not sign an arbitrary key, method or bucket supplied by the client.

Use short expiry and a generated key scoped to the account. Signing a URL does not make the object public, nor does it automatically make the capability single-use. The provider’s actual behaviour determines whether the URL can be reused before expiry.

Separate the three requests

RequestResponsibility
Browser to Next.jsAuthenticate and authorize the intended upload
Browser to storageUse the signed method, headers and object key
Browser to completion APIVerify the object and finalize its application record

Bucket CORS must permit the actual browser origin, method and headers. API CORS alone does not configure storage. A private service address is not an endpoint the visitor’s browser can reach.

Keep signing configuration server-only

Construct the S3 client inside a server-only module with the provider’s endpoint, region and credentials. A Node route can use the supported AWS SDK signing utility when compatible with the selected object store. Check the current provider documentation for supported operations and checksum or content-type requirements.

For Rivtor, use documented S3 variables and never expose S3_SECRET_ACCESS_KEY through a NEXT_PUBLIC_ variable. R2 is S3-compatible, but compatibility does not mean every AWS S3 feature is available.

Validate completion and cleanup

After upload, verify object existence, allowed metadata and ownership through the server. A client saying “done” is not evidence that the correct bytes were stored. Plan cleanup for abandoned pending uploads and prevent overwriting another user’s key.

Test a small synthetic upload, expired URL, wrong content type where constrained, unauthorized key request and browser CORS failure. Confirm downloads enforce the intended access policy. Keep presigned URLs out of shared logs because anyone possessing a still-valid capability may be able to use it.

References