tfse-app-api-v0.2/file/file_db.py

31 lines
769 B
Python
Raw Normal View History

2022-03-22 17:08:27 +08:00
import gridfs
2021-12-17 17:01:32 +08:00
import pymongo
from bson import ObjectId
from gridfs import GridFS
2022-03-22 17:08:27 +08:00
DB_HOST = "116.63.130.34"
DB_PASS = "UTlC9cCoglD1cI1*"
DB_USER = "root"
DB_PORT = "27021"
client = pymongo.MongoClient('mongodb://{}:{}@{}:{}'.format(DB_USER, DB_PASS, DB_HOST, DB_PORT))
2021-12-17 17:01:32 +08:00
2022-03-22 17:08:27 +08:00
def FIND_FILE(bucket, file_id):
2021-12-17 17:01:32 +08:00
"""
读取一个文件
Parameters:
2022-03-22 17:08:27 +08:00
bucket: 文件存储桶
file_id: 文件ID
2021-12-17 17:01:32 +08:00
Returns:
2022-03-22 17:08:27 +08:00
data 成功: 文件二进制; 失败: None
2021-12-17 17:01:32 +08:00
"""
# 实例化一个文件存储器
2022-03-22 17:08:27 +08:00
gfs = GridFS(client['文件'], collection=bucket)
try:
# 二进制读取文件
data = gfs.get(ObjectId(file_id)).read()
# 返回文件二进制流
return data
except gridfs.errors.NoFile:
return None