Environment Variables
| Variable | Description | Type | Source |
|---|---|---|---|
AWS_ACCESS_KEY_ID | AWS IAM access key ID* | server secret | AWS IAM → Users → Security credentials |
AWS_SECRET_ACCESS_KEY | AWS IAM secret access key* | server secret | AWS IAM → Users → Security credentials |
AWS_REGION | AWS region for your S3 bucket* | server safe | e.g., us-east-1, eu-west-1 |
AWS_S3_BUCKET_NAME | Name of your S3 bucket* | server safe | AWS S3 Console |
AWS_CLOUDFRONT_URL | CloudFront distribution URL (optional) | public safe | AWS CloudFront Console |
* Required variable
Step 1: Create S3 Bucket
- Go to AWS S3 Console
- Click "Create bucket"
- Enter a unique bucket name
- Select your preferred region
- Configure public access settings based on your needs
- Click "Create bucket"
Bucket Naming
S3 bucket names must be globally unique. Use a prefix like your organization name to avoid conflicts.
Step 2: Configure CORS
Enable CORS for browser uploads:
- Open your bucket in the S3 console
- Go to Permissions → Cross-origin resource sharing (CORS)
- Add the following configuration:
CORS Configuration
[
{
"AllowedHeaders": ["*"],
"AllowedMethods": ["GET", "PUT", "POST", "DELETE"],
"AllowedOrigins": ["http://localhost:3000", "https://yourdomain.com"],
"ExposeHeaders": ["ETag"]
}
]Step 3: Create IAM User
- Go to AWS IAM Console
- Navigate to Users → Create user
- Enter a username (e.g., "app-s3-user")
- Select "Attach policies directly"
- Search for and attach "AmazonS3FullAccess" (or create a more restrictive policy)
- Complete user creation
Least Privilege
For production, create a custom policy that only grants access to your specific bucket instead of using AmazonS3FullAccess.
Step 4: Create Access Keys
- Open the IAM user you created
- Go to Security credentials
- Click "Create access key"
- Select "Application running outside AWS"
- Copy both the Access key ID and Secret access key
Save Your Credentials
The secret access key is only shown once. Save it securely before closing.
Step 5: Add to .env.local
.env.local
# AWS S3 Configuration
AWS_ACCESS_KEY_ID=AKIAXXXXXXXXXXXXXXXX
AWS_SECRET_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
AWS_REGION=us-east-1
AWS_S3_BUCKET_NAME=your-bucket-name
# Optional: CloudFront CDN
# AWS_CLOUDFRONT_URL=https://dxxxxxxxxx.cloudfront.net