MongoDB Test6:测试上传下载

MongoDB Test6:测试上传下载

代码

''' Description: henggao_note version: v1.0.0 Date: 2023-08-12 10:03:06 LastEditors: henggao LastEditTime: 2023-08-12 10:04:28 ''' import time from pymongo import MongoClient from gridfs import GridFS # MongoDB连接信息 mongo_host = '192.168.92.159' mongo_port = 20000 database_name = 'test_db' # 文件路径 file_path = '.\data\data718.segy' # 连接MongoDB client = MongoClient(mongo_host, mongo_port) db = client[database_name] fs = GridFS(db) # 上传文件 def upload_file(file_path): start_time = time.time() with open(file_path, 'rb') as file: fs.put(file, filename='uploaded_file') end_time = time.time() return end_time - start_time # 下载文件 def download_file(): start_time = time.time() with open('downloaded_file.segy', 'wb') as file: grid_out = fs.get_last_version(filename='uploaded_file') file.write(grid_out.read()) end_time = time.time() return end_time - start_time if __name__ == '__main__': # 测试上传 upload_time = upload_file(file_path) print(f'上传时间:{upload_time:.4f} 秒') # 测试下载 download_time = download_file() print(f'下载时间:{download_time:.4f} 秒') client.close()
notion image