File uploader using AWS S3 and AWS CloudFront. Implemented in NestJS.
$ npm install
A S3 bucket and a CloudFront distribution connected to the bucket is required. It is recommended to keep all bucket files private and only grant access to the CloudFront distribution via an Origin Access Identity. Also, an AWS access key pair with the neccessary permissions is required.
The relevant AWS information must be set in .env
. See Configuration for more details.
All configuration is done via .env
(or system environment variables). The following configuration options must be provided:
Key | Description |
---|---|
AWS_ACCESS_KEY_ID | AWS access key ID |
AWS_SECRET_ACCESS_KEY | AWS secret access key |
REGION | AWS region |
BUCKET_NAME | S3 bucket name |
CF_DIST_ID | CloudFront distribution ID |
An example is at .env.example
.
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
Uploads a single file. The returned file name is a randomly generated UUIDv4.
POST /files
Name | Parameter Type | Data Type | Description |
---|---|---|---|
file |
form-data | file | File to be uploaded. |
curl -X POST http://localhost:3000/files \
-H "Content-Type: multipart/form-data" \
-F "[email protected]"
Status: 201 Created
{
"filename": "790d9137-e607-4192-8443-9595bcb4f4c8.txt",
"url": "https://xxxxxxxxxxx.cloudfront.net/790d9137-e607-4192-8443-9595bcb4f4c8.txt",
"size": 740,
"type": "text/plain"
}
Gets a list of all files.
GET /files
None are necessary.
curl -X GET http://localhost:3000/files
Status: 200 OK
[
{
"filename": "790d9137-e607-4192-8443-9595bcb4f4c8.txt",
"url": "https://xxxxxxxxxxx.cloudfront.net/790d9137-e607-4192-8443-9595bcb4f4c8.txt",
"size": 740,
"lastModified": "2021-05-12T08:10:10.000Z"
},
...
]
Deletes a file specified by filename.
DELETE /files/:filename
None are necessary.
curl -X DELETE http://localhost:3000/files/790d9137-e607-4192-8443-9595bcb4f4c8.txt
Status: 200 OK
Update a file, replacing its contents.
Note: It is up to the user to manage the file extension correctly when updating files.
A mismatch between ContentType
and the file extension may occur otherwise.
PATCH /files/:filename
Name | Parameter Type | Data Type | Description |
---|---|---|---|
file |
form-data | file | File to update the existing one. |
curl -X PATCH http://localhost:3000/files/790d9137-e607-4192-8443-9595bcb4f4c8.txt \
-H "Content-Type: multipart/form-data" \
-F "[email protected]"
Status: 200 OK
{
"filename": "790d9137-e607-4192-8443-9595bcb4f4c8.txt",
"url": "https://xxxxxxxxxxx.cloudfront.net/790d9137-e607-4192-8443-9595bcb4f4c8.txt",
"size": 1029,
"type": "text/plain"
}