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())
Pages
- Index of Lessons in Technology
- Index of Book Summaries
- Index of Book Lists And Downloads
- Index For Job Interviews Preparation
- Index of "Algorithms: Design and Analysis"
- Python Course (Index)
- Data Analytics Course (Index)
- Index of Machine Learning
- Postings Index
- Index of BITS WILP Exam Papers and Content
- Lessons in Investing
- Index of Math Lessons
- Index of Management Lessons
- Book Requests
- Index of English Lessons
- Index of Medicines
- Index of Quizzes (Educational)
Demo of file operations in GridFS and MongoDB using Python
Subscribe to:
Comments (Atom)
No comments:
Post a Comment