DEV Community

Cover image for Build API Server for upload files to AWS S3
Duc Tran
Duc Tran

Posted on

Build API Server for upload files to AWS S3

Build API Server for upload files to AWS S3

Image description

Full source code in here

Project structure

Overview:

.
├── ./src
├── ./Dockerfile
├── ./README.md
├── ./package-lock.json
├── ./package.json
└── ./tsconfig.json
Enter fullscreen mode Exit fullscreen mode

Detail:

src
├── config
│   ├── config.ts
│   └── index.ts
├── main.ts
├── routes
│   ├── index.ts
│   └── upload-to-S3.ts
├── services
│   ├── api.ts
│   └── index.ts
└── utils
    ├── index.ts
    └── utils.ts
Enter fullscreen mode Exit fullscreen mode

Install and build

With manual

Clone this repo:

git clone https://github.com/ductnn/api-upload-to-s3.git
cd api-upload-to-s3
Enter fullscreen mode Exit fullscreen mode

Set the enviroment variables:

cp .env.example .env

# open .env and modify the environment variables
## AWS_ACCESS_KEY=
## AWS_SECRET_KEY=
## AWS_REGION_DEFAULT=
## AWS_BUCKET_NAME=
Enter fullscreen mode Exit fullscreen mode

Install the dependencies:

npm install
Enter fullscreen mode Exit fullscreen mode

Start API Server woth command npm start:

➜  api-upload-to-s3 git:(master) npm start 

> api-upload-to-s3@1.0.0 start
> node --require ts-node/register src/main.ts

Running service:  undefined
🚀 Server started as undefined at http://localhost:5000
Enter fullscreen mode Exit fullscreen mode

Open url 127.0.0.1:5000 on browser to view result or use curl:

➜  api-upload-to-s3 git:(master) ✗ curl 127.0.0.1:5000
<h3>Build API Server for upload files to AWS S3</h3>
Enter fullscreen mode Exit fullscreen mode

Then, we use Postman with method PUT in 127.0.0.1:5000/s3/upload to
upload files.

With Dockerfile

Easy to build API with Dockerfile:

# Build images
docker build -t <YOUR-DOCKER-ID>/api-upload-to-s3:v1 -f Dockerfile .

# Push to your registry
docker push <YOUR-DOCKER-ID>/api-upload-to-s3:v1

# And run this docker image
docker run -itd -p 5000:5000 <YOUR-DOCKER-ID>/api-upload-to-s3:v1
Enter fullscreen mode Exit fullscreen mode

Contribution

Contributions are more than welcome in this project!

License

The MIT License (MIT). Please see LICENSE for more information.

Top comments (0)