Skip to content

iamKhang/storage-service

Repository files navigation

Storage Service with Supabase

A NestJS service for storing and managing files (images, videos, documents, etc.) using Supabase Storage.

Description

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.

Features

  • 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

Prerequisites

  • Node.js (v20 or higher) or Docker
  • Supabase account with Storage enabled
  • Supabase project with buckets created

Setup

Option 1: Running with Node.js

  1. Clone the repository
$ git clone <repository-url>
$ cd storage-service
  1. Install dependencies
$ npm install
  1. Configure environment variables

Copy the .env.example file to .env and update the values:

$ cp .env.example .env

Update the following variables in the .env file:

SUPABASE_URL=your_supabase_url
SUPABASE_SERVICE_KEY=your_supabase_anon_key
STORAGE_BUCKETS=avatars,courses,discussions
  1. Start the application
$ npm run start:dev

Option 2: Running with Docker

  1. Clone the repository
$ git clone <repository-url>
$ cd storage-service
  1. Configure environment variables

Create a .env file with your Supabase credentials:

$ cp .env.example .env

Update at least the following variables in the .env file:

SUPABASE_URL=your_supabase_url
SUPABASE_SERVICE_KEY=your_supabase_anon_key
  1. Build and start the Docker container
$ docker-compose up -d

The service will be available at http://localhost:3000/api/v1

Note: The .env file is used directly by docker-compose and is not included in the Docker image for security reasons.

API Endpoints

All endpoints are prefixed with /api/v1.

Upload a single file

POST /api/v1/storage/upload

Form data:

  • file: The file to upload
  • bucket (optional): The bucket to store the file in (default: 'avatars')
  • folder (optional): The folder path within the bucket

Upload multiple files

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 file information

GET /api/v1/storage/:bucket/:key

Parameters:

  • bucket: The bucket where the file is stored
  • key: The file key/path

Delete a file by path

DELETE /api/v1/storage/delete/:bucket/:filepath

Parameters:

  • bucket: The bucket where the file is stored
  • filepath: The file path within the bucket

Delete multiple files by paths

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"]
}

Delete a file by URL

POST /api/v1/storage/delete

Request body:

{
  "url": "https://your-project.supabase.co/storage/v1/object/public/avatars/user123/profile.jpg"
}

Delete multiple files by URLs

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"
  ]
}

Supabase Storage Structure

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 pictures
  • courses: For course-related files and materials
  • discussions: For files related to discussions and forums

Within each bucket, you can organize files in folders by specifying the folder parameter when uploading files.

Docker Support

This project includes Docker support for easy deployment:

  • Dockerfile: Multi-stage build for a production-ready container
  • docker-compose.yml: Configuration for running the service with environment variables from .env file

Building the Docker image manually

$ docker build -t storage-service .

Running the Docker container manually

$ docker run -p 3000:3000 \
  --env-file .env \
  storage-service

Alternatively, 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-service

License

MIT

About

Storage Service có chức năng lưu trữ toàn bộ các file/media cho toàn bộ các service của ứng dụng "Quản lý học tập thông minh" - Đồ án môn học "Kiến trúc và thiết kế trong phát triển ứng dụng phần mềm"

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors