This is the code for the backend of my cloud storage app. The frontend can be found here
- Served by Express.js
- A reverse-proxy is used to serve HTTPS on localhost by using Caddy server
- An AWS S3 bucket is used for storing files.
- A Postgres DB is used for the recording details of the files. User records are managed by Auth0; therefore, we only need to keep tabs on files/folders in our DB. The
files
table in the database has the following schema:
Table "public.files"
Column | Type | Collation | Nullable | Default
----------------+-----------------------------+-----------+----------+-------------------
id | uuid | | not null | gen_random_uuid()
name | character varying(128) | | not null |
type | filetype | | | 'file'::filetype
ownerid | text | | not null |
datecreated | timestamp(6) with time zone | | not null | now()
dateupdated | timestamp(6) with time zone | | not null | now()
parentfolderid | uuid | | |
Indexes:
"files_pkey" PRIMARY KEY, btree (id)
Check constraints:
"files_check" CHECK (parentfolderid <> id)
Foreign-key constraints:
"files_parentfolderid_fkey" FOREIGN KEY (parentfolderid) REFERENCES files(id)
Referenced by:
TABLE "files" CONSTRAINT "files_parentfolderid_fkey" FOREIGN KEY (parentfolderid) REFERENCES files(id)
The server is served by Express.js -- which operates on HTTP, and the client communicates with the server via the server's reverse proxy, that operates on HTTPS.
Files are stored in an AWS S3 bucket, and details of each file -- its owner, creation date, and more as lsited above in the summary section -- is recorded in the database. Folders have no content; they can't persist in S3, only in the database.
The files in the S3 bucket follows the naming convention USER_ID/FILE_ID
.
When a user initiates a CRUD operation, it sends a request to the Next.js server. The Next.js server then extracts the user credentials and relays the request to the backend server, that is this repository. The request of course contains the operation intended to be performed, and the user id. The server validates the user is the owner of the resource and updates the database and/or the AWS S3 bucket accordingly.