Search across all documentation pages
🤖 Read the SystemsArchitect.io Blog for over 100+ cloud architecture articles 🔥
npm install @aws-sdk/client-s3 @aws-sdk/s3-request-presigner// lib/s3.ts
import { S3Client } from "@aws-sdk/client-s3";
export const s3Client = new S3Client({
region: process.env.AWS_REGION!,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
},
});// app/api/upload/route.ts
import { PutObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { s3Client } from "@/lib/s3";
export async function POST(req: Request) {
const { filename, contentType } = await req.json();
const command = new PutObjectCommand({
Bucket: process.env.S3_BUCKET_NAME!,
Key: `uploads/${Date.now()}-${filename}`,
ContentType: contentType,
});
const url = await getSignedUrl(s3Client, command, { expiresIn: 600 });
return Response.json({ url });
}When to reach for this: You need to upload, download, or manage files in Amazon S3 from a Next.js application, using presigned URLs for secure client-side uploads.
// app/components/FileUploader.tsx
"use client";
import { useState, useRef } from "react";
export default function FileUploader() {
const [uploading, setUploading] = useState(false);
const [uploadedUrl, setUploadedUrl] = useState<string | null>(null);
const fileInputRef = useRef<HTMLInputElement>(null);
async function handleUpload() {
const file = fileInputRef.current?.files?.[0];
if (!file) return;
setUploading(true);
try {
// Step 1: Get presigned URL from our API
const res = await fetch("/api/upload", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
filename: file.name,
contentType: file.type,
}),
});
const { url } = await res.json();
// Step 2: Upload directly to S3 using the presigned URL
await fetch(url, {
method: "PUT",
headers: { "Content-Type": file.type },
body: file,
});
// Extract the permanent URL (without query params)
const permanentUrl = url.split("?")[0];
setUploadedUrl(permanentUrl);
} catch (error) {
console.error("Upload failed:", error);
} finally {
setUploading(false);
}
}
return (
<div className="p-6 max-w-md mx-auto space-y-4">
<div>
<input
ref={fileInputRef}
type="file"
accept="image/*,.pdf,.doc,.docx"
className="block w-full text-sm"
/>
</div>
<button
onClick={handleUpload}
disabled={uploading}
className="bg-blue-600 text-white px-4 py-2 rounded disabled:opacity-50"
>
{uploading ? "Uploading..." : "Upload"}
</button>
{uploadedUrl && (
<p className="text-sm text-green-600">
Uploaded: <a href={uploadedUrl} className="underline">View file</a>
</p>
)}
</div>
);
}// app/actions/s3-actions.ts
"use server";
import {
ListObjectsV2Command,
GetObjectCommand,
DeleteObjectCommand,
} from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { s3Client } from "@/lib/s3";
const BUCKET = process.env.S3_BUCKET_NAME!;
export async function listFiles(prefix: string = "uploads/") {
const command = new ListObjectsV2Command({
Bucket: BUCKET,
Prefix: prefix,
MaxKeys: 50,
});
const response = await s3Client.send(command);
return (
response.Contents?.map((item) => ({
key: item.Key!,
size: item.Size!,
lastModified: item.LastModified!.toISOString(),
})) ?? []
);
}
export async function getDownloadUrl(key: string) {
const command = new GetObjectCommand({
Bucket: BUCKET,
Key: key,
});
return getSignedUrl(s3Client, command, { expiresIn: 3600 });
}
export async function deleteFile(key: string) {
const command = new DeleteObjectCommand({
Bucket: BUCKET,
Key: key,
});
await s3Client.send(command);
}What this demonstrates:
@aws-sdk/client-s3)send() it via the client@aws-sdk/s3-request-presigner generates time-limited signed URLs for any S3 command/ as a delimiter but S3 is flat storage - folders are just key prefixesServer Action file upload (for smaller files):
"use server";
import { PutObjectCommand } from "@aws-sdk/client-s3";
import { s3Client } from "@/lib/s3";
export async function uploadFile(formData: FormData) {
const file = formData.get("file") as File;
const buffer = Buffer.from(await file.arrayBuffer());
const command = new PutObjectCommand({
Bucket: process.env.S3_BUCKET_NAME!,
Key: `uploads/${Date.now()}-${file.name}`,
Body: buffer,
ContentType: file.type,
});
await s3Client.send(command);
}Download and stream a file:
// app/api/download/[key]/route.ts
import { GetObjectCommand } from "@aws-sdk/client-s3";
import { s3Client } from "@/lib/s3";
export async function GET(
req: Request,
{ params }: { params: { key: string } }
) {
const command = new GetObjectCommand({
Bucket: process.env.S3_BUCKET_NAME!,
Key: decodeURIComponent(params.key),
});
const response = await s3Client.send(command);
const stream = response.Body as ReadableStream;
return new Response(stream, {
headers: {
"Content-Type": response.ContentType ?? "application/octet-stream",
"Content-Length": String(response.ContentLength),
},
});
}Copy objects between buckets:
import { CopyObjectCommand } from "@aws-sdk/client-s3";
const command = new CopyObjectCommand({
Bucket: "destination-bucket",
CopySource: "source-bucket/path/to/file.pdf",
Key: "new-path/file.pdf",
});
await s3Client.send(command);PutObjectCommandInput, GetObjectCommandInput, etc.GetObjectCommandOutput.Body is typed as StreamingBlobPayloadOutputTypes - cast to ReadableStream in serverless environmentsS3ClientConfig type for custom client configurationimport type {
PutObjectCommandInput,
ListObjectsV2CommandOutput,
} from "@aws-sdk/client-s3";
const params: PutObjectCommandInput = {
Bucket: "my-bucket",
Key: "file.txt",
Body: "Hello, World!",
};CORS errors on client-side upload - The browser blocks PUT requests to S3. Fix: Configure CORS on the S3 bucket to allow PUT from your domain. Add AllowedOrigins, AllowedMethods: ["PUT"], and AllowedHeaders: ["Content-Type"].
Presigned URL expired - URLs expire after the configured expiresIn seconds. Fix: Generate URLs just before use. Default to 600 seconds (10 minutes) for uploads; do not generate URLs far in advance.
Large file uploads failing - Files over 5GB cannot use single PUT. Fix: Use multipart upload with @aws-sdk/lib-storage Upload class for files over 100MB.
Missing Content-Type - Files uploaded without Content-Type get application/octet-stream. Fix: Always pass ContentType in the PutObjectCommand and in the client-side fetch headers.
Next.js body size limit - Server Action uploads are limited to the Next.js body size limit (default 1MB). Fix: Use presigned URL uploads for larger files, or increase experimental.serverActions.bodySizeLimit in next.config.js.
Credential exposure - Never import @aws-sdk/client-s3 in client components. Fix: All AWS SDK usage must be in API routes, Server Components, or Server Actions.
| Library | Best For | Trade-off |
|---|---|---|
| @aws-sdk/client-s3 | Full S3 API access | Requires AWS account and CORS setup |
| Vercel Blob | Simple file storage on Vercel | Vercel-only, less control |
| Uploadthing | File uploads with React hooks | Abstraction over S3, less flexibility |
| Cloudflare R2 | S3-compatible, no egress fees | Separate SDK or S3 compatibility mode |
| Supabase Storage | Integrated with Supabase | Tied to Supabase ecosystem |
getSignedUrl and returns itfetch(url, { method: "PUT", body: file }) to upload directly to S3PUT from your domainAllowedHeaders: ["Content-Type"] and your domain in AllowedOriginsPutObjectCommand is limited to 5GB@aws-sdk/lib-storage and its Upload class for multipart uploadsexperimental.serverActions.bodySizeLimit in next.config.jsconst command = new GetObjectCommand({ Bucket: BUCKET, Key: key });
const response = await s3Client.send(command);
return new Response(response.Body as ReadableStream, {
headers: {
"Content-Type": response.ContentType ?? "application/octet-stream",
"Content-Length": String(response.ContentLength),
},
});ContentType in PutObjectCommand, S3 defaults to application/octet-streamContentType: file.type in both the command and the client-side fetch headersimport type { PutObjectCommandInput } from "@aws-sdk/client-s3";
const params: PutObjectCommandInput = {
Bucket: "my-bucket",
Key: "file.txt",
Body: "Hello, World!",
ContentType: "text/plain",
};Each command has a corresponding *CommandInput and *CommandOutput type.
Body is typed as StreamingBlobPayloadOutputTypes, not ReadableStreamresponse.Body as ReadableStreamundefined before using the bodyconst command = new ListObjectsV2Command({
Bucket: BUCKET,
Prefix: "uploads/images/",
MaxKeys: 50,
});
const response = await s3Client.send(command);
const files = response.Contents ?? [];S3 is flat storage -- "folders" are just key prefixes separated by /.
"use server";
import { DeleteObjectCommand } from "@aws-sdk/client-s3";
import { s3Client } from "@/lib/s3";
export async function deleteFile(key: string) {
await s3Client.send(
new DeleteObjectCommand({ Bucket: BUCKET, Key: key })
);
}Reviewed by Chris St. John·Last updated Jul 16, 2026
🤖 Read the SystemsArchitect.io Blog for over 100+ cloud architecture articles 🔥