Setup & Configuration
Learn how to onfigure environment variables and initializing the S3 client. It details the steps to create an S3 bucket and obtain credentials, ensuring a secure and validated setup.
To use AWS S3 for file storage, you must configure environment variables in your .env file, validated by t3-env to prevent runtime errors. The S3 client, initialized in lib/amazon, enables file uploads and URL generation, integrated with your Next.js application.
Steps to Set Up AWS S3
Create an AWS Account
- Sign up at AWS and log in to the AWS Management Console.
- Ensure you have access to IAM and S3 services.
Create an S3 Bucket
- Navigate to S3 in the AWS Console and create a new bucket (e.g.,
plainform-dev-storage). - Choose a region (e.g.,
us-east-1) and configure permissions (e.g., public read for URLs or private with signed URLs).
Obtain AWS Credentials
- Go to IAM > Users in the AWS Console, create a user with S3 access (e.g.,
s3-upload-user). - Generate an Access Key ID and Secret Access Key under Security Credentials.
- Attach a policy granting
s3:PutObjectands3:GetObjectpermissions for your bucket.
Configure Environment Variables
- Add these to your
.env(or.env.local) file:
| Env Variable | Type | Default |
|---|---|---|
AWS_ACCESS_KEY_ID | string | - |
AWS_SECRET_ACCESS_KEY | string | - |
AWS_S3_ENDPOINT | string | https://<yourAppName>.s3.<region>.amazonaws.com |
AWS_S3_REGION | string | - |
AWS_S3_BUCKET | string | - |
- Ensure
.envis in.gitignoreto protect sensitive keys.
Initialize the S3 Client
- Plainform includes an S3 client in
@/lib/amazon/s3.ts. Example:
import { S3Client } from '@aws-sdk/client-s3';
export const s3 = new S3Client({
region: process.env.AWS_S3_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
},
});- The client is ready for file uploads once environment variables are validated.
Verify Configuration
- Run npm run dev to validate environment variables via
t3-env(inenv.mjs). - If validation fails, check your credentials and bucket name in the AWS Console.
For advanced configurations, such as bucket policies or CORS settings, consult the AWS S3 Documentation.
How is this guide ?
Last updated on