For image storage in Next.js, Cloudinary is often the easiest choice due to its built-in optimizations and CDN. AWS S3 gives you more control but requires additional setup for transformations. Here's how to implement Cloudinary:
1. Install the SDK: `npm install cloudinary`
2. Set up environment variables in `.env.local`:
```bash
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_key
CLOUDINARY_API_SECRET=your_secret
```
3. Create an API route for uploads:
```javascript
import { v2 as cloudinary } from 'cloudinary';
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
export default async function handler(req, res) {
if (req.method === 'POST') {
try {
const result = await cloudinary.uploader.upload(req.body.image);
res.status(200).json(result);
} catch (error) {
res.status(500).json({ error: error.message });
}
}
}
```
4. On the client side, use a file input with FormData to send to your API route.
For AWS S3, you'd need to set up IAM permissions, create buckets, and handle uploads via the AWS SDK, which adds complexity but may be better for large-scale applications. Consider using the Next.js Image component with either service for optimal performance.