AWS S3 Storage
Switching from Cloudinary to AWS S3 for file uploads in StarterKitPro.
Alternative Storage Provider
While StarterKitPro defaults to Cloudinary, you can easily switch to using AWS S3 (Simple Storage Service) for handling file uploads if it better suits your needs or existing infrastructure. This guide details the steps to make the switch.
Switching from Cloudinary to S3 involves installing the AWS SDK, updating environment variables, replacing the storage helper library, and modifying the server actions.
1. Setup
Follow these steps to configure your project for AWS S3.
Package Management
- Install AWS SDK: Add the necessary AWS SDK v3 packages for S3 interaction.
Terminal - Remove Cloudinary (Optional): If you are completely switching and no longer need Cloudinary, you can remove its package:
Terminal
Environment Variables
- Remove Cloudinary Keys: Comment out or delete the Cloudinary variables from your
.env.local
file. - Add S3 Keys: Add your AWS S3 credentials and bucket details.
Obtaining AWS Credentials & Bucket Setup
You need an S3 bucket and an IAM user with appropriate permissions:
- Create an S3 Bucket:
- Log in to your AWS Management Console.
- Navigate to the S3 service.
- Create a new bucket, choosing a unique name and your desired AWS region. Note these down for your
.env.local
. - Permissions: Configure bucket permissions. For publicly accessible files like avatars, you might need to adjust block public access settings and add a bucket policy granting public read (
s3:GetObject
) access. Start with private access and adjust as needed.
- Create an IAM User:
- Navigate to the IAM service in the AWS Console.
- Create a new IAM user (e.g.,
starterkitpro-s3-user
). - Attach Policies: Grant this user permissions to interact with your specific S3 bucket. You can start with
AmazonS3FullAccess
for ease of setup during development, but for production, create a more restrictive custom policy allowing only necessary actions (s3:PutObject
,s3:GetObject
,s3:DeleteObject
) on your specific bucket resource (arn:aws:s3:::your_s3_bucket_name/*
). - Generate Access Keys: Create an access key for this user. Copy the Access Key ID and Secret Access Key immediately and store them securely. You won't be able to see the Secret Access Key again. Add these to your
.env.local
.
AWS Credential Security
Treat your AWS Access Key ID and Secret Access Key like passwords. Never commit them to version control. Ensure .env.local
is in your .gitignore
. For deployments on AWS, consider using IAM roles for better security.
2. Centralized File Management
StarterKitPro uses a centralized helper module for storage operations. When switching to S3, you need to replace the Cloudinary helper with an S3 equivalent.
- Remove Cloudinary Helper: Delete the file
lib/cloudinary.ts
. - Create S3 Helper: Create a new file
lib/s3.ts
and add the following code, which provides similar functionality using the AWS SDK:
This lib/s3.ts
module now provides the uploadToCloud
and removeFromCloud
functions, interacting with your configured S3 bucket.
3. Update Secure Server Actions
The server actions in actions/file-actions.ts
orchestrate the file operations and include security checks. You only need to update the import path to use the new S3 helper.
-
Open
actions/file-actions.ts
. -
Change the import statement:
actions/file-actions.ts
Because the function names (uploadToCloud
, removeFromCloud
) and their basic purpose remain the same between lib/cloudinary.ts
and lib/s3.ts
, no further changes should be needed within the action logic itself, provided the S3 implementation handles parameters and returns URLs as expected.
Security with Auth.js Remains
All file management actions within actions/file-actions.ts
continue to be protected using Auth.js, regardless of the storage backend (Cloudinary or S3). Only authenticated users can perform these operations.
By following these steps—installing the AWS SDK, updating environment variables, replacing lib/cloudinary.ts
with lib/s3.ts
, and modifying the import in actions/file-actions.ts
—you have successfully switched StarterKitPro's file storage backend to AWS S3.