Very simple API that receives uploaded files and saves them on disk.
Use either nginx or caddy to proxy forward requests to API's listening port and to later serve saved files.
| Option | Description |
|---|---|
routePrefix |
If requests to the API are coming from e.g. nginx, set this to the path that preceedes requests, e.g. if the API is available under https://example.com/api set to /api |
bindAddress |
IP and port on which the API will listen |
serveURLRoot |
String that will be prepended to the file name generated by the API - basically path under which the uploaded files are available under |
uploadDirectory |
Path to the directory in which API will save uploaded files |
uploadPassword |
String that has to be provided in Authorization header while uploading files |
slugLength |
Number of characters the generated file name will have |
# clone and build the project
sudo mkdir /opt/file-upload-api && sudo chown $USER:$USER /opt/file-upload-api
git clone https://github.com/zneix/file-upload-api /opt/file-upload-api && cd /opt/file-upload-api
go build ./cmd/api/
# copy and edit config.json file
cp config-dist.json config.json
# vim config.json
# install the systemd unit file
sudo cp /opt/file-upload-api/file-upload-api.service /etc/systemd/system/
# edit user's name in the systemd unit file
sudo sed -i "s/exampleuser/$USER/g" /etc/systemd/system/file-upload-api.service
# start the API
sudo systemctl enable --now file-upload-api.servicePut the following inside server block in your domain's configuration file
location /api {
client_max_body_size 50M; # this defines limit for uploaded file size, adjust as needed
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:9401;
}
server {
server_name cdn.example.com;
root /home/user/cdn;
autoindex off;
listen [::]:443 ssl;
listen 443 ssl;
# example ssl configuration, adjust as needed
ssl_certificate /home/user/fullchain.cer;
ssl_certificate_key /home/user/example.com.key;
}
Requests should include configured password in Authorization header, be of type POST and include file in the file form field.
curl -sL -H "Authorization: dankpassword123" --form "file=@/home/user/Pictures/FeelsDankMan.png" https://example.com/api/uploadIf anything is unclear or instructions are inaccurate, feel free to open an issue.