A Node.js REST API for managing file uploads, storage, and categorization using AWS S3 and PostgreSQL.
- File Upload & Storage: Upload files to AWS S3 with automatic URL generation
- File Management: CRUD operations for files with metadata
- Category Management: Organize files by categories
- Database Integration: PostgreSQL with Prisma ORM
- Cloud Storage: AWS S3 integration for scalable file storage
- RESTful API: Clean REST endpoints for all operations
- Node.js (v14 or higher)
- PostgreSQL database
- AWS S3 bucket
- AWS credentials (Access Key ID and Secret Access Key)
- 
Clone the repository git clone <repository-url> cd Files_API 
- 
Install dependencies npm install 
- 
Set up environment variables Create a .envfile in the root directory:# Database Configuration DATABASE_URL="postgresql://username:password@localhost:5432/database_name" # Server Configuration PORT=8090 # AWS S3 Configuration AWS_ACCESS_KEY=your_aws_access_key_here AWS_SECRET_ACCESS_KEY=your_aws_secret_access_key_here AWS_REGION=your_aws_region_here AWS_S3_BUCKET_NAME=your_bucket_name_here # Optional: Custom S3 endpoint (for S3-compatible services) # AWS_S3_ENDPOINT=https://s3.amazonaws.com # JWT Configuration (if using authentication) JWT_ACCESS_SECRET=your_jwt_access_secret JWT_REFRESH_SECRET=your_jwt_refresh_secret ACCESS_TOKEN_AGE=15m REFRESH_TOKEN_AGE=7d 
- 
Set up the database # Generate Prisma client npx prisma generate # Run database migrations npx prisma migrate dev --name init 
- 
Start the server npm start 
The server will start on http://localhost:8090
POST /files/upload
Content-Type: multipart/form-data
Body:
- file: (file) - The file to upload (PDF only)
- file: (JSON string) - File metadata
  {
    "description": "File description",
    "user_id": "user123",
    "category_id": "cat456"
  }Note: Only one file per user is allowed. Uploading a new file will replace the existing one.
GET /files/get-all?role=ADMINISTRATORQuery Parameters:
- role(required): Must be "ADMINISTRATOR" or "PROFESSOR"
GET /files/get?file_id=file123&role=ADMINISTRATORQuery Parameters:
- file_id(required): The ID of the file to retrieve
- role(required): Must be "ADMINISTRATOR" or "PROFESSOR"
GET /files/get-user-id?user_id=user123Query Parameters:
- user_id(required): The ID of the user whose files to retrieve
PUT /files/update
Content-Type: multipart/form-data
Body:
- file: (file, optional) - New file to replace existing
- file: (JSON string) - Updated file metadata
  {
    "user_id": "user123",
    "description": "Updated description",
    "category_id": "cat789"
  }DELETE /files/delete?user_id=user123&file_id=file123Query Parameters:
- user_id(required): The ID of the user who owns the file
- file_id(required): The ID of the file to delete
POST /category/create
Content-Type: application/json
{
  "category": {
    "name": "Category Name",
    "description": "Category description (optional)"
  },
  "role": "ADMINISTRATOR"
}Note: Only users with ADMINISTRATOR role can create categories.
GET /category/get-allPUT /category/update
Content-Type: application/json
{
  "category": {
    "id": "cat123",
    "name": "Updated Category Name",
    "description": "Updated description"
  },
  "role": "ADMINISTRATOR"
}Note: Only users with ADMINISTRATOR role can update categories.
DELETE /category/delete?id=cat123&role=ADMINISTRATORQuery Parameters:
- id(required): The ID of the category to delete
- role(required): Must be "ADMINISTRATOR"
Note: Only users with ADMINISTRATOR role can delete categories.
model Files {
  id          String    @id @default(cuid())
  url         String    @db.VarChar(500)
  description String?   @db.VarChar(255)
  user_id     String    @db.VarChar(255)
  category_id String    @db.VarChar(255)
  created_at  DateTime  @default(now())
  updated_at  DateTime  @updatedAt
  category    Categories  @relation(fields: [category_id], references: [id], onDelete: Cascade)
}model Categories {
  id          String    @id @default(cuid())
  name        String    @db.VarChar(50)
  description String?   @db.VarChar(255)
  created_at  DateTime  @default(now())
  updated_at  DateTime  @updatedAt
  files Files[]
}- Create an S3 bucket in your AWS account
- Configure bucket permissions for public read access (if needed)
- Create IAM user with S3 permissions
- Get access credentials and add them to your .envfile
- Create PostgreSQL database
- Update DATABASE_URL in your .envfile
- Run migrations to create tables
Files_API/
βββ src/
β   βββ controllers/          # Request handlers
β   β   βββ files/           # File-related controllers
β   β   βββ category/        # Category-related controllers
β   βββ services/            # Business logic
β   β   βββ files/           # File services
β   β   βββ category/        # Category services
β   βββ routes/              # API routes
β   βββ lib/                 # Utility libraries
β   β   βββ files/           # File handling utilities
β   β   βββ prisma/          # Database connection
β   β   βββ errors/          # Error handling
β   βββ routes/              # Route definitions
βββ prisma/                  # Database schema and migrations
βββ index.js                 # Application entry point
βββ package.json
curl -X POST http://localhost:8090/files/upload \
  -F "file=@document.pdf" \
  -F 'file={"description":"My thesis document","user_id":"user123","category_id":"cat456"}'curl "http://localhost:8090/files/get-all?role=ADMINISTRATOR"curl "http://localhost:8090/files/get?file_id=file123&role=ADMINISTRATOR"curl "http://localhost:8090/files/get-user-id?user_id=user123"curl -X PUT http://localhost:8090/files/update \
  -F "file=@updated_document.pdf" \
  -F 'file={"user_id":"user123","description":"Updated thesis document","category_id":"cat789"}'curl -X DELETE "http://localhost:8090/files/delete?user_id=user123&file_id=file123"curl -X POST http://localhost:8090/category/create \
  -H "Content-Type: application/json" \
  -d '{"category":{"name":"Computer Science","description":"CS related documents"},"role":"ADMINISTRATOR"}'curl "http://localhost:8090/category/get-all"curl -X PUT http://localhost:8090/category/update \
  -H "Content-Type: application/json" \
  -d '{"category":{"id":"cat123","name":"Updated Computer Science","description":"Updated CS documents"},"role":"ADMINISTRATOR"}'curl -X DELETE "http://localhost:8090/category/delete?id=cat123&role=ADMINISTRATOR"- Environment Variables: Never commit your .envfile
- AWS Credentials: Use IAM roles with minimal required permissions
- File Validation: Implement file type and size validation as needed
- Access Control: Add authentication middleware for production use
- 
Database Connection Error - Check your DATABASE_URLin.env
- Ensure PostgreSQL is running
- Run npx prisma generateandnpx prisma migrate dev
 
- Check your 
- 
S3 Upload Error - Verify AWS credentials in .env
- Check bucket name and region
- Ensure bucket permissions are correct
 
- Verify AWS credentials in 
- 
Port Already in Use - Change PORTin.envfile
- Or kill the process using port 8090
 
- Change 
This project is licensed under the ISC License.
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
For support and questions, please open an issue in the repository.