A NestJS service for storing and managing files (images, videos, documents, etc.) using Supabase Storage.
This service provides a set of RESTful API endpoints for uploading, retrieving, and managing files in Supabase Storage. It supports multiple buckets for different file types and provides a clean, organized structure for file storage.
- Upload single files to Supabase Storage
- Upload multiple files at once
- Organize files in different buckets (avatars, documents, videos, images)
- Support for folder structure within buckets
- Automatic generation of unique file names
- File validation (size, type)
- Retrieve file information
- Delete files
- Node.js (v20 or higher) or Docker
- Supabase account with Storage enabled
- Supabase project with buckets created
- Clone the repository
$ git clone <repository-url>
$ cd storage-service- Install dependencies
$ npm install- Configure environment variables
Copy the .env.example file to .env and update the values:
$ cp .env.example .envUpdate the following variables in the .env file:
SUPABASE_URL=your_supabase_url
SUPABASE_SERVICE_KEY=your_supabase_anon_key
STORAGE_BUCKETS=avatars,courses,discussions
- Start the application
$ npm run start:dev- Clone the repository
$ git clone <repository-url>
$ cd storage-service- Configure environment variables
Create a .env file with your Supabase credentials:
$ cp .env.example .envUpdate at least the following variables in the .env file:
SUPABASE_URL=your_supabase_url
SUPABASE_SERVICE_KEY=your_supabase_anon_key
- Build and start the Docker container
$ docker-compose up -dThe service will be available at http://localhost:3000/api/v1
Note: The
.envfile is used directly by docker-compose and is not included in the Docker image for security reasons.
All endpoints are prefixed with /api/v1.
POST /api/v1/storage/upload
Form data:
file: The file to uploadbucket(optional): The bucket to store the file in (default: 'avatars')folder(optional): The folder path within the bucket
POST /api/v1/storage/upload-multiple
Form data:
files: The files to upload (array)bucket(optional): The bucket to store the files in (default: 'avatars')folder(optional): The folder path within the bucket
GET /api/v1/storage/:bucket/:key
Parameters:
bucket: The bucket where the file is storedkey: The file key/path
DELETE /api/v1/storage/delete/:bucket/:filepath
Parameters:
bucket: The bucket where the file is storedfilepath: The file path within the bucket
DELETE /api/v1/storage/delete-multiple/:bucket
Parameters:
bucket: The bucket where the files are stored
Request body:
{
"paths": ["path/to/file1.jpg", "path/to/file2.pdf"]
}POST /api/v1/storage/delete
Request body:
{
"url": "https://your-project.supabase.co/storage/v1/object/public/avatars/user123/profile.jpg"
}POST /api/v1/storage/delete-multiple
Request body:
{
"urls": [
"https://your-project.supabase.co/storage/v1/object/public/avatars/user123/profile1.jpg",
"https://your-project.supabase.co/storage/v1/object/public/avatars/user123/profile2.jpg"
]
}The service is designed to work with multiple buckets in Supabase Storage. Each bucket can be used for different types of files:
avatars: For user profile picturescourses: For course-related files and materialsdiscussions: For files related to discussions and forums
Within each bucket, you can organize files in folders by specifying the folder parameter when uploading files.
This project includes Docker support for easy deployment:
Dockerfile: Multi-stage build for a production-ready containerdocker-compose.yml: Configuration for running the service with environment variables from.envfile
$ docker build -t storage-service .$ docker run -p 3000:3000 \
--env-file .env \
storage-serviceAlternatively, you can specify environment variables directly:
$ docker run -p 3000:3000 \
-e SUPABASE_URL=your_supabase_url \
-e SUPABASE_SERVICE_KEY=your_supabase_anon_key \
-e STORAGE_BUCKETS=avatars,courses,discussions \
storage-serviceMIT