Secure file upload handling for Google Antigravity IDE
# File Upload Patterns for Google Antigravity
Master file upload handling in Google Antigravity IDE. This guide covers multipart uploads, validation, cloud storage, and security patterns.
## Server-Side Upload Handler
```typescript
import { NextRequest, NextResponse } from "next/server";
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({ region: process.env.AWS_REGION });
const ALLOWED_TYPES = ["image/jpeg", "image/png", "image/webp", "application/pdf"];
const MAX_SIZE = 10 * 1024 * 1024; // 10MB
export async function POST(request: NextRequest) {
const formData = await request.formData();
const file = formData.get("file") as File;
if (!file) {
return NextResponse.json({ error: "No file provided" }, { status: 400 });
}
if (!ALLOWED_TYPES.includes(file.type)) {
return NextResponse.json({ error: "Invalid file type" }, { status: 400 });
}
if (file.size > MAX_SIZE) {
return NextResponse.json({ error: "File too large" }, { status: 400 });
}
const buffer = Buffer.from(await file.arrayBuffer());
const key = `uploads/${Date.now()}-${file.name}`;
await s3.send(new PutObjectCommand({
Bucket: process.env.S3_BUCKET,
Key: key,
Body: buffer,
ContentType: file.type
}));
return NextResponse.json({ url: `https://${process.env.S3_BUCKET}.s3.amazonaws.com/${key}` });
}
```
## Presigned URL Upload
```typescript
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
export async function getUploadUrl(filename: string, contentType: string) {
const key = `uploads/${Date.now()}-${filename}`;
const command = new PutObjectCommand({
Bucket: process.env.S3_BUCKET,
Key: key,
ContentType: contentType
});
const signedUrl = await getSignedUrl(s3, command, { expiresIn: 3600 });
return { uploadUrl: signedUrl, key };
}
```
## Best Practices
1. **Validate file types** - Check MIME and extension
2. **Limit file size** - Prevent resource exhaustion
3. **Use presigned URLs** - Direct uploads to storage
4. **Scan for malware** - Security scanning
Google Antigravity IDE provides file upload scaffolding.This File Upload prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their file upload implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For File Upload projects, consider mentioning your framework version, coding style, and any specific libraries you're using.