def read_gridfs_file(filename): bucket = GridFSBucket(proc_client.perftest) temp = tempfile.TemporaryFile() try: bucket.download_to_stream_by_name(filename, temp) finally: temp.close()
def read_gridfs_file(filename): bucket = GridFSBucket(proc_client.perftest) temp = tempfile.TemporaryFile() try: bucket.download_to_stream_by_name(filename, temp) finally: temp.close()
class DB_GridFS(): def __init__(self): db = MongoClient().GEOSIM_GRIDFS self.fs = GridFSBucket(db) # @timer def upload(self, obj_name, filepath, metadata={'origin_hash': '_hash'}): grid_in = self.fs.open_upload_stream( obj_name, chunk_size_bytes=CHUNK_SIZE, metadata=metadata) with open(filepath, mode='rb') as dst: grid_in.write(dst) # grid_in.write(file_like_obj) grid_in.close() # uploaded on close # @timer def download(self, obj_name, filepath): with open(filepath, mode='wb') as dst: self.fs.download_to_stream_by_name(obj_name, dst) # @timer def delete(self, keyname): cur = self.fs.find({"filename": keyname}) for item in cur: self.fs.delete(item._id)
class DB_GridFS(DBMongoBase): """Класс работы с MongoDB Gridfs """ def __init__(self, config_params, database): """Получаем базу, GridFSBucket, коллекцию :param config_params: dict словарь конфигурационных парметров dict(host='10.205.33.221', port=27017, username=None, password=None) :param database: string название базы данных MongoDB """ # Client for a MongoDB instance # client = MongoClient(**config_params) super().__init__(config_params) # print(self.client.test_database) # Get a database by client and name self.db = self.client[database] # получаем GridFSBucket self.fs = GridFSBucket(self.db) # получаем коллекцию fs.files - стандартная коллекция GridFSBucket self.collection = self.db.fs.files print(self.collection) self.meta = self.client['main'].projects print(self.meta) def upload(self, obj_name, filepath, metadata={'origin_hash': '_hash'}): """метод загрузки данных в БД :param obj_name: string название объекта value поля filename :param filepath: file_object file_object с данными, отправляемыми в БД :param metadata: dict метаданные записываемые в БД return: None """ # GridIn instance grid_in = self.fs.open_upload_stream(obj_name, chunk_size_bytes=CHUNK_SIZE, metadata=metadata) # with open(filepath, mode='rb') as dst: # Перемотаем буфер к началу filepath.seek(0) # запись данных grid_in.write(filepath) # grid_in.write(file_like_obj) # закрываем instance при этом инициируется отправка еще не отправленных данных grid_in.close() # uploaded on close # обнулим буфер не удаляя объекта filepath.truncate(0) return grid_in._id def download(self, obj_name, filepath, version=-1): """ :param obj_name: string название объекта value поля filename :param filepath: file_object куда будет загружены данные :param version: номер версии Revision numbers are defined as follows: 0 = the original stored file 1 = the first revision 2 = the second revision etc… -2 = the second most recent revision -1 = the most recent revision :return: None """ # with open(filepath, mode='wb') as dst: filepath.seek(0) # download_to_stream_by_name ничего не возвращает self.fs.download_to_stream_by_name(obj_name, filepath, revision=version) filepath.seek(0) def delete(self, keyname): """Удалиение документа по filename :param keyname: string filename :param return: """ # получаем итератор ответа на запрос cur = self.fs.find({"filename": keyname}) # проходим по ответам и удаляем соответствующие документы for item in cur: self.fs.delete(item._id) def insert_one(self, data_dict): return self.collection.insert_one(data_dict).inserted_id def insert_many(self, data_dict): self.collection.insert_many(data_dict) def find(self, collection, where, select={"_id": 0}): return collection.find(where, select, no_cursor_timeout=True)