|
| 1 | +# AWS S3 Setup Guide |
| 2 | + |
| 3 | +Your app now uses a hybrid storage system: |
| 4 | +- **Primary**: Cloudinary (tries first) |
| 5 | +- **Fallback**: AWS S3 (if Cloudinary fails) |
| 6 | + |
| 7 | +## Quick Setup |
| 8 | + |
| 9 | +### 1. Install AWS SDK |
| 10 | + |
| 11 | +```bash |
| 12 | +bun add @aws-sdk/client-s3 |
| 13 | +``` |
| 14 | + |
| 15 | +### 2. Create AWS Account |
| 16 | + |
| 17 | +Go to https://aws.amazon.com/ and sign up (free tier available) |
| 18 | + |
| 19 | +### 3. Create S3 Bucket |
| 20 | + |
| 21 | +1. Go to AWS Console → S3 |
| 22 | +2. Click "Create bucket" |
| 23 | +3. Bucket name: `stremora-videos` (must be globally unique) |
| 24 | +4. Region: `us-east-1` (or your preferred region) |
| 25 | +5. **Uncheck "Block all public access"** ⚠️ Important! |
| 26 | +6. Click "Create bucket" |
| 27 | + |
| 28 | +### 4. Set Bucket Policy (Make Files Public) |
| 29 | + |
| 30 | +1. Go to your bucket → Permissions → Bucket Policy |
| 31 | +2. Click "Edit" and paste this (replace `stremora-videos` with your bucket name): |
| 32 | + |
| 33 | +```json |
| 34 | +{ |
| 35 | + "Version": "2012-10-17", |
| 36 | + "Statement": [ |
| 37 | + { |
| 38 | + "Effect": "Allow", |
| 39 | + "Principal": "*", |
| 40 | + "Action": "s3:GetObject", |
| 41 | + "Resource": "arn:aws:s3:::stremora-videos/*" |
| 42 | + } |
| 43 | + ] |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +3. Click "Save changes" |
| 48 | + |
| 49 | +### 5. Create IAM User (Get API Keys) |
| 50 | + |
| 51 | +1. Go to AWS Console → IAM → Users |
| 52 | +2. Click "Add users" |
| 53 | +3. Username: `stremora-uploader` |
| 54 | +4. Select "Access key - Programmatic access" |
| 55 | +5. Click "Next: Permissions" |
| 56 | +6. Click "Attach existing policies directly" |
| 57 | +7. Search and select: **AmazonS3FullAccess** |
| 58 | +8. Click through to create user |
| 59 | +9. **IMPORTANT**: Copy the Access Key ID and Secret Access Key (you won't see them again!) |
| 60 | + |
| 61 | +### 6. Add to .env File |
| 62 | + |
| 63 | +Add these to your `.env`: |
| 64 | + |
| 65 | +```env |
| 66 | +# AWS S3 Configuration (Fallback Storage) |
| 67 | +AWS_REGION=us-east-1 |
| 68 | +AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE |
| 69 | +AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY |
| 70 | +AWS_S3_BUCKET_NAME=stremora-videos |
| 71 | +``` |
| 72 | + |
| 73 | +### 7. Test It! |
| 74 | + |
| 75 | +Restart your server: |
| 76 | + |
| 77 | +```bash |
| 78 | +bun run dev |
| 79 | +``` |
| 80 | + |
| 81 | +Upload a video and watch the logs: |
| 82 | + |
| 83 | +``` |
| 84 | +📤 [Storage] Attempting Cloudinary upload first... |
| 85 | +⚠️ [Storage] Cloudinary failed, trying S3 fallback... |
| 86 | +✅ [Storage] S3 fallback successful! |
| 87 | +📦 Storage provider: s3 |
| 88 | +``` |
| 89 | + |
| 90 | +## How It Works |
| 91 | + |
| 92 | +``` |
| 93 | +Upload Video |
| 94 | + ↓ |
| 95 | +Try Cloudinary First |
| 96 | + ↓ |
| 97 | +Success? → Use Cloudinary ✅ |
| 98 | + ↓ |
| 99 | +Failed? → Try S3 Fallback |
| 100 | + ↓ |
| 101 | +Success? → Use S3 ✅ |
| 102 | + ↓ |
| 103 | +Both Failed? → Error ❌ |
| 104 | +``` |
| 105 | + |
| 106 | +## Cost |
| 107 | + |
| 108 | +### AWS Free Tier (First 12 Months): |
| 109 | +- 5 GB storage |
| 110 | +- 20,000 GET requests |
| 111 | +- 2,000 PUT requests |
| 112 | +- 100 GB data transfer out |
| 113 | + |
| 114 | +### After Free Tier: |
| 115 | +- Storage: ~$0.023/GB/month |
| 116 | +- Requests: $0.0004 per 1,000 GET, $0.005 per 1,000 PUT |
| 117 | +- Transfer: $0.09/GB |
| 118 | + |
| 119 | +**Example**: 100 videos (10GB) + 10k views/month = ~$1.50/month |
| 120 | + |
| 121 | +Much cheaper than Cloudinary's paid plans! |
| 122 | + |
| 123 | +## Troubleshooting |
| 124 | + |
| 125 | +### "Access Denied" Error |
| 126 | + |
| 127 | +**Problem**: Bucket policy not set correctly |
| 128 | + |
| 129 | +**Solution**: |
| 130 | +1. Go to bucket → Permissions → Bucket Policy |
| 131 | +2. Make sure the policy allows public read (see step 4 above) |
| 132 | +3. Verify "Block all public access" is OFF |
| 133 | + |
| 134 | +### "Credentials Not Found" Error |
| 135 | + |
| 136 | +**Problem**: AWS credentials not in .env |
| 137 | + |
| 138 | +**Solution**: |
| 139 | +1. Check `.env` has AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY |
| 140 | +2. Restart your server after adding credentials |
| 141 | + |
| 142 | +### Files Upload But Can't Access |
| 143 | + |
| 144 | +**Problem**: Bucket policy doesn't allow public read |
| 145 | + |
| 146 | +**Solution**: |
| 147 | +1. Check bucket policy (step 4) |
| 148 | +2. Verify ACL is set to "public-read" in upload |
| 149 | +3. Try accessing the URL directly in browser |
| 150 | + |
| 151 | +### "Bucket Not Found" Error |
| 152 | + |
| 153 | +**Problem**: Bucket name mismatch or wrong region |
| 154 | + |
| 155 | +**Solution**: |
| 156 | +1. Verify bucket name in .env matches exactly |
| 157 | +2. Check region is correct |
| 158 | +3. Make sure bucket exists in AWS Console |
| 159 | + |
| 160 | +## Monitoring Usage |
| 161 | + |
| 162 | +Check your AWS usage: |
| 163 | +1. Go to AWS Console → Billing Dashboard |
| 164 | +2. View "Free Tier" usage |
| 165 | +3. Set up billing alerts (recommended!) |
| 166 | + |
| 167 | +## Security Best Practices |
| 168 | + |
| 169 | +1. **Never commit .env to git** (already in .gitignore) |
| 170 | +2. **Use IAM user** (not root account) |
| 171 | +3. **Limit permissions** (only S3, not full AWS access) |
| 172 | +4. **Rotate keys** periodically |
| 173 | +5. **Set up billing alerts** to avoid surprises |
| 174 | + |
| 175 | +## Need Help? |
| 176 | + |
| 177 | +- AWS Documentation: https://docs.aws.amazon.com/s3/ |
| 178 | +- AWS Free Tier: https://aws.amazon.com/free/ |
| 179 | +- Support: AWS has excellent documentation and community support |
| 180 | + |
| 181 | +Your hybrid storage system is now ready! Videos will automatically fall back to S3 when Cloudinary has issues. 🚀 |
0 commit comments