Claude Code के साथ Implement File Upload कैसे करें
Claude Code का उपयोग करके implement file upload सीखें। Practical code examples और step-by-step guidance शामिल है।
fileuploadfeaturesのimplementationにClaude Code use करना
fileuploadはvalidation、署名付きURL、プlogレスdisplay、画像リsize आदिimplementation項目がज़्यादाfeatures है।Claude Codeに一括して依頼する बातで、セキュアで使いやすいuploadfeaturesをefficientlybuild किया जा सकता है。
署名付きURLによるダイレクトupload
> S3の署名付きURLを使ったfileuploadをimplement करो。
> serverを経由せず、client से直接S3にuploadする構成で。
> filesizeは10MBनिम्नलिखित、画像とPDFのみ許可して。
serverサイド(署名付きURL発行)
// src/app/api/upload/route.ts
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { NextRequest, NextResponse } from 'next/server';
import { nanoid } from 'nanoid';
const s3 = new S3Client({ region: 'us-east-1' });
const ALLOWED_TYPES = ['image/jpeg', 'image/png', 'image/webp', 'application/pdf'];
const MAX_SIZE = 10 * 1024 * 1024; // 10MB
export async function POST(req: NextRequest) {
const { fileName, fileType, fileSize } = await req.json();
// validation
if (!ALLOWED_TYPES.includes(fileType)) {
return NextResponse.json(
{ error: '許可されていないfile形式です' },
{ status: 400 }
);
}
if (fileSize > MAX_SIZE) {
return NextResponse.json(
{ error: 'filesizeは10MBनिम्नलिखितにしてください' },
{ status: 400 }
);
}
const ext = fileName.split('.').pop();
const key = `uploads/${nanoid()}.${ext}`;
const command = new PutObjectCommand({
Bucket: process.env.S3_BUCKET!,
Key: key,
ContentType: fileType,
ContentLength: fileSize,
});
const signedUrl = await getSignedUrl(s3, command, { expiresIn: 300 });
return NextResponse.json({
uploadUrl: signedUrl,
fileUrl: `https://${process.env.S3_BUCKET}.s3.us-east-1.amazonaws.com/${key}`,
key,
});
}
clientサイド(プlogレス付きupload)
import { useState, useCallback } from 'react';
interface UploadState {
progress: number;
uploading: boolean;
error: string | null;
fileUrl: string | null;
}
export function useFileUpload() {
const [state, setState] = useState<UploadState>({
progress: 0,
uploading: false,
error: null,
fileUrl: null,
});
const upload = useCallback(async (file: File) => {
setState({ progress: 0, uploading: true, error: null, fileUrl: null });
try {
// 1. 署名付きURL fetch
const res = await fetch('/api/upload', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
fileName: file.name,
fileType: file.type,
fileSize: file.size,
}),
});
if (!res.ok) {
const { error } = await res.json();
throw new Error(error);
}
const { uploadUrl, fileUrl } = await res.json();
// 2. S3に直接upload(プlogレス付き)
await new Promise<void>((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', (e) => {
if (e.lengthComputable) {
setState(prev => ({ ...prev, progress: Math.round((e.loaded / e.total) * 100) }));
}
});
xhr.addEventListener('load', () => {
if (xhr.status >= 200 && xhr.status < 300) resolve();
else reject(new Error('Upload failed'));
});
xhr.addEventListener('error', () => reject(new Error('Network error')));
xhr.open('PUT', uploadUrl);
xhr.setRequestHeader('Content-Type', file.type);
xhr.send(file);
});
setState({ progress: 100, uploading: false, error: null, fileUrl });
return fileUrl;
} catch (err) {
setState(prev => ({
...prev,
uploading: false,
error: (err as Error).message,
}));
throw err;
}
}, []);
return { ...state, upload };
}
uploadcomponent
function FileUploader() {
const { progress, uploading, error, fileUrl, upload } = useFileUpload();
const [dragOver, setDragOver] = useState(false);
const handleDrop = useCallback(async (e: React.DragEvent) => {
e.preventDefault();
setDragOver(false);
const file = e.dataTransfer.files[0];
if (file) await upload(file);
}, [upload]);
const handleFileSelect = useCallback(async (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (file) await upload(file);
}, [upload]);
return (
<div>
<div
onDragOver={(e) => { e.preventDefault(); setDragOver(true); }}
onDragLeave={() => setDragOver(false)}
onDrop={handleDrop}
className={`border-2 border-dashed rounded-lg p-8 text-center transition-colors
${dragOver ? 'border-blue-500 bg-blue-50' : 'border-gray-300'}
${uploading ? 'pointer-events-none opacity-50' : 'cursor-pointer'}`}
>
<p className="text-gray-600">
fileをドラッグ&ドロップ、またはクリックして選択
</p>
<p className="mt-1 text-sm text-gray-400">
JPEG, PNG, WebP, PDF(10MBनिम्नलिखित)
</p>
<input
type="file"
accept="image/jpeg,image/png,image/webp,application/pdf"
onChange={handleFileSelect}
className="hidden"
id="file-input"
/>
<label htmlFor="file-input" className="mt-4 inline-block cursor-pointer rounded bg-blue-600 px-4 py-2 text-white">
fileを選択
</label>
</div>
{uploading && (
<div className="mt-4">
<div className="h-2 rounded-full bg-gray-200">
<div
className="h-full rounded-full bg-blue-600 transition-all"
style={{ width: `${progress}%` }}
/>
</div>
<p className="mt-1 text-sm text-gray-500">{progress}%</p>
</div>
)}
{error && <p className="mt-2 text-sm text-red-600">{error}</p>}
{fileUrl && (
<p className="mt-2 text-sm text-green-600">
upload完了
</p>
)}
</div>
);
}
画像リsizeのprocessing
uploadされた画像をLambdaで自動リsizeするprocessingもClaude Code को requestでき है।
// lambda/resize-image.ts
import { S3Event } from 'aws-lambda';
import { S3Client, GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3';
import sharp from 'sharp';
const s3 = new S3Client({});
const SIZES = [
{ suffix: 'thumb', width: 200, height: 200 },
{ suffix: 'medium', width: 800 },
{ suffix: 'large', width: 1600 },
];
export async function handler(event: S3Event) {
for (const record of event.Records) {
const bucket = record.s3.bucket.name;
const key = decodeURIComponent(record.s3.object.key);
const { Body } = await s3.send(new GetObjectCommand({ Bucket: bucket, Key: key }));
const buffer = Buffer.from(await Body!.transformToByteArray());
for (const size of SIZES) {
const resized = await sharp(buffer)
.resize(size.width, size.height, { fit: 'cover' })
.webp({ quality: 80 })
.toBuffer();
const resizedKey = key.replace(/\.[^.]+$/, `-${size.suffix}.webp`);
await s3.send(new PutObjectCommand({
Bucket: bucket,
Key: resizedKey,
Body: resized,
ContentType: 'image/webp',
}));
}
}
}
Summary
Claude Code का उपयोग करके、fileuploadfeaturesを署名付きURL、プlogレスdisplay、validation、画像リsize तक含めてefficientlyimplementationでき है।securityを考慮したimplementationを一括でgenerateできるのが強み है।AWSintegrationके details के लिएAWSdeployのautomationもदेखें。個人Projectでのutilization法は個人developmentを爆速にする方法で紹介してい है।
Claude Codeके details के लिएAnthropicofficial documentationदेखें。
Related Posts
Claude Code से अपने Side Projects को Supercharge कैसे करें [Examples के साथ]
Claude Code से personal development projects को dramatically speed up करना सीखें। Real-world examples और idea से deployment तक practical workflow शामिल है।
Claude Code से Refactoring कैसे Automate करें
Claude Code से efficiently code refactoring automate करना सीखें। Real-world projects के लिए practical prompts और concrete refactoring patterns शामिल हैं।
Claude Code के साथ Complete CORS Configuration Guide
Claude Code का उपयोग करके complete CORS configuration guide सीखें। Practical tips और code examples शामिल हैं।