from pprint import pprint import pymongo from pymongo import MongoClient import gridfs client = MongoClient() client = MongoClient('mongodb://localhost:27017/') db = client['local'] fs = gridfs.GridFS(db) for grid_out in fs.find({"filename": "grid_fs.txt"}, no_cursor_timeout=True): data = grid_out.read() print(data) OUT: b'Hello.\n' # INSERTION fileID = fs.put(open(r'/home/ashish/Desktop/workspace/jupyter/exp/files_1/insert_test.txt', 'rb'), filename="insert_test.txt") out = fs.get(fileID) print(type(out)) print(out.length) print(out.read()) OUT: class 'gridfs.grid_file.GridOut' 16 b'Insertion test.\n' fileID = fs.put( open(r'/home/ashish/Desktop/workspace/jupyter/exp/files_1/blue_rose.jfif', 'rb'), filename="blue_rose.jfif" ) out = fs.get(fileID) print(type(out)) print(out.length) OUT: class 'gridfs.grid_file.GridOut' 6717 # RETRIEVAL f_out_path = r'/home/ashish/Desktop/workspace/jupyter/exp/files_1/blue_rose_out.jfif' with open(f_out_path, 'wb') as f: f.write(out.read())
Demo of file operations in GridFS and MongoDB using Python
Subscribe to:
Posts (Atom)
No comments:
Post a Comment