Author: hao.nguyen
Use the package manager pip to install.
cd project dir
python3 -m venv env
source env/bin/activate
pip3 install -r requirements.txt⚠️⚠️️⚠️Note: if get any error when install requirements | remove pkg_resources==0.0.0 in requirements.txt and try again
- Rename
.envexampleto.env - Change config app in
.env - Change
APP_ENVtodevfor test andproductfor product
Create DB
- Collection
cdn-userexample data of user
{
"_id": ObjectId("6128c754196b000025003e98"),
"user": "admin",
"password": "hao@123",
"active": true,
"max_upload_size": 200
}- Normal
python3 app.py - With Gunicorn
gunicorn --bind=<your_ip>:<your_port> --workers=2 --threads=3 app:app
- Upload file from
direct urlorform/data - Access file
directorhide url - Block bad extensions file
- Manage file size upload for each accout
- Upload with custom dir
- Keep name of file upload
may be overide old file same name - Logging to graylog
Input field:
file: file or url | (rerequired)type: type of file defaultfile| acceptfileor directurl| (optional)is_keep_name: default when upload file app will rename file | for keep original file name when upload, option this field1| accept0or1| (optional)custom_dir: input your directory want to upload | default file will upload into root directory of account | accept multiple subdirectories and name valid this regex[0-9a-zA-Z]
Response:
status:successorerrordesc: Descreption of errororDescreption of file upload if success
Example Request:
- cURL:
curl --location --request POST 'http://localhost:9999/upload' \ --header 'Authorization: Basic YWRtaW46aGFvQDEyMw==' \ --form 'file=@"/C:/Users/haong/OneDrive/Desktop/My Recycle Bin/file gui/qcdongtesttrung.xlsx"'
- PHP cURL:
<?php $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => 'http://localhost:9999/upload', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_POSTFIELDS => array('file'=> new CURLFILE('/C:/Users/haong/OneDrive/Desktop/My Recycle Bin/file gui/qcdongtesttrung.xlsx')), CURLOPT_HTTPHEADER => array( 'Authorization: Basic YWRtaW46aGFvQDEyMw==' ), )); $response = curl_exec($curl); curl_close($curl); echo $response;
- Python request
import requests url = "http://localhost:9999/upload" payload={} files=[ ('file',('qcdongtesttrung.xlsx',open('/C:/Users/haong/OneDrive/Desktop/My Recycle Bin/file gui/qcdongtesttrung.xlsx','rb'),'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')) ] headers = { 'Authorization': 'Basic YWRtaW46aGFvQDEyMw==' } response = requests.request("POST", url, headers=headers, data=payload, files=files) print(response.text)
Example Response:
- Success:
{ "desc": { "custom_dir": "/hao/exel/", "direct_url": "http://localhost:9999/file/admin/hao/exel/5M84N38X_30082021175602.xlsx", "file_size": 244872, "hide_url": "http://localhost:9999/file/hao/exel/612cb9423e5509492a8ed009", "is_keep_name": false, "mimetype": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "raw_file_name": "qcdongtesttrung.xlsx", "save_file_name": "5M84N38X_30082021175602.xlsx", "user_name": "admin" }, "status": "success" } - Error:
{ "desc": "invalid custom_dir, only accept [a-zA-Z] and '/'", "status": "error" }