-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
46 lines (36 loc) · 1.43 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from fastapi import FastAPI,File, UploadFile
from fastapi.responses import HTMLResponse
from typing import Any, Dict, AnyStr, List, Union
import uvicorn,os
import aiofiles
app=FastAPI(title="FileUpload")
@app.get("/")
async def main():
content = """<html> <title>Teste UpLoad</title> <body> Teste UpLoad </body> </html> """
return HTMLResponse(content=content)
@app.post("/sizefile/")
async def sizefile(files: List[bytes] = File(...)):
return {"file_sizes": [len(file) for file in files]}
@app.post("/uploadfiles/")
async def create_uploadfiles(files: List[UploadFile] = File(...)):
path='uploads/'
if not os.path.isdir(path):
os.mkdir(path)
print('create',os.getcwd())
for k in files:
async with aiofiles.open(path+k.filename, 'wb') as out_file:
while content := await k.read(1024):
await out_file.write(content)
return {"filenames": [file.filename for file in files]}
@app.post("/uploadfile")
async def create_uploadfile(input_data: UploadFile = File(...)):
path='upload/'
if not os.path.isdir(path):
os.mkdir(path)
print('create',os.getcwd())
async with aiofiles.open(path+input_data.filename, 'wb') as out_file:
while content := await input_data.read(1024):
await out_file.write(content)
return {"filename": input_data.filename}
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=8000, debug=True)