This project is a secure web application built with Node.js that allows users to upload files to an AWS S3 bucket. It features a professional user interface, user authentication, and seamless AWS S3 integration for managing file uploads.
- Frontend: HTML5, CSS3 (Bootstrap/Tailwind CSS/W3CSS), JavaScript
- Backend: Node.js, Express.js, AWS SDK for JavaScript, Multer (for file upload handling)
- AWS Services: S3, IAM (Identity and Access Management)
Before starting, make sure you have the following installed:
- Node.js (version 14 or higher) – Download Node.js
- npm (Node Package Manager) – npm comes with Node.js
- AWS Account – You'll need an AWS account to set up the S3 bucket and IAM user for access.
The application can be deployed locally by following the setup instructions.
First, clone the repository to your local machine:
git clone https://github.com/your-username/aws-s3-file-upload.git
cd aws-s3-file-upload
Navigate to the project directory and install the necessary dependencies by running:
npm install
Create a .env file in the root of the project directory and add the following environment variables:
- AWS_ACCESS_KEY_ID=your_aws_access_key_id
- AWS_SECRET_ACCESS_KEY=your_aws_secret_access_key
- AWS_BUCKET_NAME=your_s3_bucket_name
- AWS_REGION=your_aws_region
- SESSION_SECRET=your_session_secret_key
- AWS_ACCESS_KEY_ID: Your AWS access key ID.
- AWS_SECRET_ACCESS_KEY: Your AWS secret access key.
- AWS_REGION: The region of your S3 bucket.
- AWS_BUCKET_NAME: The name of your S3 bucket.
- Log into the AWS Management Console.
- Go to S3 and create a new bucket (or use an existing one) to store the uploaded files.
- Ensure that the IAM user you are using has the necessary permissions to upload to this S3 bucket.
- Attach the AmazonS3FullAccess policy to the IAM user for simplicity (or create a custom policy if needed).
- Make sure to note the Bucket Name and Region for your .env file.
Now you can start the application by running:
npm run dev
As we have used nodemon for running node application
Open a web browser and go to:
http://localhost:3000
- URL:
/auth/login
- Method:
POST
- Description: Authenticates users with a username and PIN.
- Request Body:
{ "username": "your-username", "pin": "your-pin" }
{ "success": true, "message": "Login successful" }
{ "error": "Invalid username or PIN" }
URL: /upload Method: POST Description: Uploads a file to the configured AWS S3 bucket.
Authorization: Bearer token (provided after login).
Form-data with key file containing the file to be uploaded. Response: Success: { "success": true, "message": "File uploaded successfully" } Error: { "error": "File upload failed. Please try again." }
- User sessions are managed using a session-based authentication system.
- Secure login using predetermined credentials.
- Managed securely using environment variables.
- No hard-coded credentials in the codebase.
- Only files of type PDF, JPEG, and PNG are allowed.
- Maximum file size: 10MB.
- Graceful handling for invalid file types and upload failures.
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
We have to make sure that we have the correct AWS credentials and IAM permissions configured in our .env file. The application should work without further changes to AWS if your credentials are correct.
-
AWS S3 Bucket Setup:
- Create an S3 bucket in your AWS account.
- Enable public access if needed or configure specific access policies.
- Note the bucket name and region for environment variables.
-
AWS IAM Role and Policies:
- Create an IAM user with programmatic access.
- Attach the following policy to allow file uploads:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "s3:PutObject", "Resource": "arn:aws:s3:::your-bucket-name/*" } ] }
- Save the access key and secret key for environment variables.
-
Environment Variables:
- Create a
.env
file in the root of your project with the following keys:AWS_ACCESS_KEY_ID=your-access-key-id AWS_SECRET_ACCESS_KEY=your-secret-access-key AWS_REGION=your-region AWS_BUCKET_NAME=your-bucket-name SESSION_SECRET=your-session-secret
- Replace the placeholders with your actual credentials and configurations.
- Create a
-
Node.js Dependencies:
- Install all required dependencies:
npm install
- Install all required dependencies:
-
Start the Application:
- Run the application locally:
npm start
- The server will start on the specified port (default is
3000
).
- Run the application locally:
-
Testing the Application:
- Access the application in your browser at
http://localhost:3000
. - Test login, file upload, and error handling to ensure everything works as expected.
- Access the application in your browser at