-
Notifications
You must be signed in to change notification settings - Fork 9
/
example_gridfs.py
44 lines (32 loc) · 1.03 KB
/
example_gridfs.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
# @Author: Huang Sizhe <huangsizhe>
# @Date: 08-Apr-2017
# @Email: [email protected]
# @Last modified by: huangsizhe
# @Last modified time: 08-Apr-2017
# @License: MIT
from sanic import Sanic
from sanic.response import json,text
from sanic_mongo import GridFS
app = Sanic(__name__)
mongo_uri = "mongodb://{host}:{port}/{database}".format(
database='test',
port=27017,
host='localhost'
)
GridFS.SetConfig(app,test_fs=(mongo_uri,"fs"))
GridFS(app)
@app.get('/pics')
async def get(request):
cursor = app.GridFS["test_fs"].find()
result = [{i._id:i.name} async for i in cursor]
return json({"result":result})
@app.post('/pics')
async def new(request):
doc = request.files.get('file')
async with app.GridFS["test_fs"].open_upload_stream(filename=doc.name,
metadata={"contentType": doc.type}) as gridin:
object_id = gridin._id
await gridin.write(doc.body)
return json({'object_id': str(object_id)})
if __name__ == "__main__":
app.run(host='127.0.0.1', port=8000,debug=True)